objective-c高德地圖時時定位


本人開發的開發者技術變現資源聚集地,大家支持下,下面是網址

https://www.baiydu.com

 

 

這篇隨筆是對上一遍servlet接口的實現。

一、項目集成高德地圖

應為我這個項目使用了cocopods這個第三方庫管理工具,所以只需要很簡單的步驟,就能將高德地圖集成到項目中,如果你沒使用過這工具,那就直接下載對應的sdk導入即可,因為這個工具的安裝還是比較麻煩的,需要翻牆。 既然我用的這種方式,下面我就貼相關的使用接圖。

 

如果你項目中已經安裝好了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處理。

     

 本人創業做的一款androidApp, 下載量已經有2000多萬,各種當前熱門的網絡手機獎勵紅包全部集成,另外還有熱門電影和淘寶高額優惠券!很適合各類型的用戶。

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM