更新Mapview Xcode中的位置

时间:2015-11-25 07:19:58

标签: ios objective-c xcode mkmapview cllocationmanager

在我目前的项目中。

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

基本上在每次Objective c更改后打开应用程序后,我需要{{1}}中的呼叫Web服务的用户位置。此外,我希望在应用程序处于后台状态时运行相同的进程。

提前致谢

2 个答案:

答案 0 :(得分:2)

  1. 您必须在应用程序启动时创建CLLocationManager的对象并设置它的委托
  2. 添加以下代码以获取用户的当前位置

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

      CLLocationDistance米= [newLocation distanceFromLocation:oldLocation];

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

答案 1 :(得分: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);

}