在Mapview Xcode中更新位置

在我目前的项目。

我需要每50 meter用户移动用户的位置。

所以基本上每打开一个50 meter变化后,我需要在Objective c调用Web服务的用户位置。 另外我希望当应用程序处于后台状态时运行相同的进程。

提前致谢

设置您的位置跟踪

 //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); } 
  1. 应用程序启动时必须创buildCLLocationManager对象,并将其设置为委托

添加下面的代码来获取用户的当前位置

 CLLocationManager *locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; locationManager.desiredAccuracy = kCLLocationAccuracyBest; [locationManager startUpdatingLocation]; 
  1. 现在添加一个名为didUpdateToLocation的CLLocationManagaer的委托,并添加下面的代码。

    CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation];

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