2015-11-25 34 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 
    }