这篇随笔是对上一遍servlet接口的实现。
应为我这个项目使用了cocopods这个第三方库管理工具,所以只需要很简单的步骤,就能将高德地图集成到项目中,如果你没使用过这工具,那就直接下载对应的sdk导入即可,因为这个工具的安装还是比较麻烦的,需要FQ。 既然我用的这种方式,下面我就贴相关的使用接图。
如果你项目中已经安装好了cocopods那么你只需直接在podfile中增加 pod 'Amap2DMap' 和 pod 'AmapSearch' 这两句即可, 第一个涉及到的地图功能主要是定位,地图展示...
第二个涉及到的主要功能是GPS定位,这里为什么要使用第二个? 因为我们传递参数时需要给servlet传递一个当前城市的名称,就需要它来执行一个逆地理编码获取城市名,
然后在打开打开终端 cd 拖入项目文件夹 pod install稍等即可安装。
二、时时定位
引入高德地图两个类。
#import <AMapSearchKit/AMapSearchAPI.h>
#import <MAMapKit/MAMapKit.h>
下面截图所需代理及涉及到的参数。。
上面截图中关于我们实现这个功能需要使用到的代理只有一个高德地图的:MAMapViewDelegate
1、在didFinishLaunchingWithOptions授权高德地图
[MAMapServices sharedServices].apiKey = (NSString *)APIKey;
2、启动时时定位,间隔时间为15秒 类中有时时定位的代理,但是被我使用代码控制了的,因为系统自带的不能控制定位间隔时间
_nstimer=[NSTimer scheduledTimerWithTimeInterval:15 target:self selector:@selector(timerFired:) userInfo:nil repeats:NO];
3、MAMapViewDelegate 代理实现的方法,服务人员时时定位,经纬度提交高德云图库
↓下面这个方法提示使用APP的人员是否允许该应用使用定位功能。
- (void)mapViewWillStartLocatingUser:(MAMapView *)mapView { if(![CLLocationManager locationServicesEnabled]){ UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"定位失败" message:@"请在手机设置中开启定位功能/n开启步骤:设置 > 隐私 > 位置 > 定位服务" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alertView show]; return; }else{ if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedAlways) { UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"定位失败" message:@"请在手机设置中开启定位功能/n开启步骤:设置 > 隐私 > 位置 > 定位服务下《***》应用" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alertView show]; return; } } } View Code
↓下面的代码是时时定位的,15秒一次,并且将定位后的经纬度提交到高德云图库中去
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation { _mapView=nil; [_nstimer invalidate]; if ([WDUser currentUser].userType==2) { longitude=[NSString stringWithFormat:@"%f%@", mapView.userLocation.coordinate.longitude,@","];//@"106.546128";// latitude= [NSString stringWithFormat:@"%f", mapView.userLocation.coordinate.latitude]; locationGroup=[NSString stringWithFormat:@"%@%@",longitude,latitude]; NSString *healtherName=[NSString stringWithFormat:@"%d,康复师",[WDUser currentUser].healterLoginTypes]; NSMutableDictionary *postParameterDic=[[NSMutableDictionary alloc]init]; [postParameterDic setObject:@"?" forKey:@"tableid"]; [postParameterDic setObject:@"?" forKey:@"key"]; NSMutableDictionary *dataDictionary=[[NSMutableDictionary alloc]init]; NSString *yunClouldReturnId=[NSString stringWithFormat:@"%d",[WDUser currentUser].healtherClouldMapId]; [dataDictionary setObject:yunClouldReturnId forKey:@"_id"]; [dataDictionary setObject:healtherName forKey:@"_name"]; [dataDictionary setObject:locationGroup forKey:@"_location"];//点后6位 格式示例:104.394729,31.125698 NSString *dateStr=[AFDataDefine shareGetStr:dataDictionary]; [postParameterDic setObject:dateStr forKey:@"data"]; // 设置返回格式 AFHTTPRequestOperationManager *postManager = [AFHTTPRequestOperationManager manager]; postManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"charset=UTF-8",@"Content-Type",@"application/x-www-form-urluncoded",nil];; postManager.requestSerializer = [AFJSONRequestSerializer serializer]; postManager.responseSerializer = [AFHTTPResponseSerializer serializer]; [postManager GET:@"http://yuntuapi.amap.com/datamanage/data/update" parameters:postParameterDic success:^(AFHTTPRequestOperation *operation, id responseObject) { // NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]; [_nstimer invalidate]; _nstimer=[NSTimer scheduledTimerWithTimeInterval:15 target:self selector:@selector(timerFired:) userInfo:nil repeats:NO]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { // NSLog(@"1"); [_nstimer invalidate]; _nstimer=[NSTimer scheduledTimerWithTimeInterval:15 target:self selector:@selector(timerFired:) userInfo:nil repeats:NO]; } ]; } else { [_nstimer invalidate]; _nstimer=[NSTimer scheduledTimerWithTimeInterval:15 target:self selector:@selector(timerFired:) userInfo:nil repeats:NO]; } } View Code
↓下面的代码是timer控件调用的方法
-(void)timerFired:(NSTimer *)timer{ [_nstimer invalidate]; _mapView=[[MAMapView alloc]init]; _mapView.userTrackingMode = 1, _mapView.showsUserLocation = YES; _mapView.userTrackingMode = MAUserTrackingModeNone; _mapView.delegate=self; } View Code
↓下面的代码是应用进入后台,以及从后台进入前台时的定位处理
- (void)applicationDidEnterBackground:(UIApplication *)application {//在后台 _mapView=nil; UIApplication *app = [UIApplication sharedApplication]; __block UIBackgroundTaskIdentifier bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ dispatch_async(dispatch_get_main_queue(), ^{ if (bgTask != UIBackgroundTaskInvalid) { [app endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; } }); }]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [_nstimer invalidate]; _nstimer=[NSTimer scheduledTimerWithTimeInterval:15 target:self selector:@selector(timerFired:) userInfo:nil repeats:NO]; [[NSRunLoop currentRunLoop] addTimer:_nstimer forMode:NSRunLoopCommonModes]; [[NSRunLoop currentRunLoop] run]; dispatch_async(dispatch_get_main_queue(), ^{ if (bgTask != UIBackgroundTaskInvalid) { [app endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; } }); }); } - (void)applicationWillEnterForeground:(UIApplication *)application {//在前台 _mapView=nil; [_nstimer invalidate]; _nstimer=[NSTimer scheduledTimerWithTimeInterval:15 target:self selector:@selector(timerFired:) userInfo:nil repeats:NO]; [[NSRunLoop currentRunLoop] addTimer:_nstimer forMode:NSRunLoopCommonModes]; } View Code
上面的方法实现的主要功能就是时时定位,然后网高德云图中传递服务人员的位置经纬度,它们针对的是服务人员。
4:被服务人员跳转服务人员页面,提交经纬度和地理位置给 servlet接口处理。
这个方法针对的是被服务人员,因为被服务人员也需要提供自己当前经纬度,但是不是时时提供,只是跳转到服务人员页面时,才提供,而且是直接调用servlet写的接口,
而且被服务人员还需要提供当前城市的名称,所以这里还需要使用一个GPS定位,通过经纬度去查询地理位置名称。
↓下面的代码是申明高德地图
_longitude=[[NSString alloc]init]; _latitude=[[NSString alloc]init]; computeFrame=self.LeftButton.frame; self.mapView=[[MAMapView alloc]initWithFrame:CGRectMake(0, 0, 320, 500)]; self.mapView.userTrackingMode = 1, _mapView.showsUserLocation = YES; self.mapView.userTrackingMode = MAUserTrackingModeNone; self.mapView.delegate=self; View Code
↓下面的代码主要是GPS定位获取城市名称。
- (void)mapViewDidStopLocatingUser:(MAMapView *)mapView { _longitude=[NSString stringWithFormat:@"%f",mapView.userLocation.location.coordinate.longitude];//@"106.546128";// _latitude= [NSString stringWithFormat:@"%f",mapView.userLocation.location.coordinate.latitude]; //@"29.559153"; [WDUser currentUser].longitude=_longitude; [WDUser currentUser].latitude=_latitude; [[WDUser currentUser] saveUserInfo]; _search = [[AMapSearchAPI alloc] initWithSearchKey:@"?" Delegate:self]; AMapReGeocodeSearchRequest *regeoRequest = [[AMapReGeocodeSearchRequest alloc] init]; regeoRequest.searchType = AMapSearchType_ReGeocode; regeoRequest.location =[AMapGeoPoint locationWithLatitude:[_latitude floatValue] longitude:[_longitude floatValue]]; regeoRequest.radius = 10000; regeoRequest.requireExtension = YES; //发起逆地理编码 [_search AMapReGoecodeSearch: regeoRequest]; } //逆编码查询代理 - (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response { if(response.regeocode != nil) { _compantAddress=response.regeocode.addressComponent; _currentCityName=_compantAddress.province; _currentCityName= [_currentCityName stringByReplacingOccurrencesOfString:@"市" withString:@""]; _currentCityName= [_currentCityName stringByReplacingOccurrencesOfString:@"省" withString:@""]; [ WDUser currentUser].currentCity=_currentCityName; [[WDUser currentUser] saveUserInfo]; [SVProgressHUD dismiss]; } else{ showMessage(@"请检查网是否畅通!"); } } - (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation { mapView.showsUserLocation = NO; }//联系客户 View Code
最后服务人员将这些获取的参数提交到servlet处理。