2015-11-25 81 views
0

在我當前的項目中。在Mapview Xcode中更新位置

我需要用戶的位置在每個50 meter用戶移動。

所以基本上在打開應用程序後,每50 meter更改我需要在Objective c呼叫Web服務的用戶位置。另外我希望當應用程序處於後臺狀態時運行相同的進程。

在此先感謝

回答

1

您的位置設置軌道

//create location manager object 
locationManager = [[CLLocationManager alloc] init]; 

//there will be a warning from this line of code 
[locationManager setDelegate:self]; 

//and we want it to be as accurate as possible 
//regardless of how much time/power it takes 
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; 

//set the amount of metres travelled before location update is made 
[locationManager setDistanceFilter:50]; 

,並添加

if ([CLLocationManager locationServicesEnabled]) { 
    [self.locationManager startUpdatingLocation]; 
} 

更新

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { 
CLLocation *location = locations.lastObject; 
NSLog(@"%@", location.description); 

//In here you get all details like 

    NSLog(@"latitude = %@",location.coordinate.latitude); 
    NSLog(@"longitude = %@",location.coordinate.longitude); 
    NSLog(@"altitude = %@",location.altitude); 
    NSLog(@"horizontalAccuracy = %@",location.horizontalAccuracy); 
    NSLog(@"verticalAccuracy = %@",location.verticalAccuracy); 
    NSLog(@"timestamp = %@",location.timestamp); 
    NSLog(@"speed = %@",location.speed); 
    NSLog(@"course = %@",location.course); 

} 
+0

我將[的LocationManager setDistanceFilter:50]; 在我的代碼 –

+0

但是當位置大於50米時調用哪個方法? –

+0

像每隔50米更改只是打開警報視圖並顯示消息「您的位置更改50米」 –

2
  1. 你必須讓應用程序啓動時CLLocationManager的對象,並設置它的委託

添加下面的代碼來獲取用戶當前位置

CLLocationManager *locationManager = [[CLLocationManager alloc] init]; 
locationManager.delegate = self; 
locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
[locationManager startUpdatingLocation]; 
  • 現在添加didUpdateToLocation的CLLocationManagaer的委託並在其中添加以下代碼。

    CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation];

    if(meters==50) 
    { 
        // CALL YOU WEBSERVICE 
    }