在 Mapview 中更新位置 Xcode

Update Location in 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. 你必须在应用程序启动时创建 CLLocationManager 的对象并设置它的委托

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

CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
  1. 现在添加 CLLocationManagaer 的委托 didUpdateToLocation 并在其中添加以下代码。

    CLLocationDistance 米=[newLocationdistanceFromLocation:oldLocation];

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