2014-05-20 40 views
1

我有一个按钮,当您按下按钮时,您将获得该位置。它只适用于初次启动,而不是后续使用。下面的代码IBAction仅在首次发布时获取核心位置位置

- (IBAction)getLocationCoordinates:(UIButton *)sender { 
if ([CLLocationManager locationServicesEnabled]) { 
    self.locationManager = [[CLLocationManager alloc]init]; 
    self.locationManager.delegate = self; 
    [self.locationManager startUpdatingLocation]; 
} 


} 

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ 

CLLocation *location = [locations lastObject]; 
[self.locationManager stopUpdatingLocation]; 
NSLog(@"%f %f", location.coordinate.longitude, location.coordinate.latitude); 
self.locationProperty = location; 

} 

回答

0

如果位置没有显着变化(不移动),您将不会从didUpdateLocation获取新位置。

我有iOS 7的前景和背景位置更新的详细完整解决方案,您可以访问:Background Location Services not working in iOS 7了解更多详情。

该链接将引导您在Github上提供完整的解决方案,同时也是详细的博客文章。

+0

很酷。我会看看。当我改变模拟位置时,我没有得到新的位置,我得到了旧位置。说我选择墨西哥城,然后我换到纽约,我又得到墨西哥城的数据。我可能也没有正确实现CLLocationManager。 – noobsmcgoobs

+0

我相信你测试位置的方式不正确。你的物理设备必须移动一小段距离,因为iOS使用加速计来知道设备是否实际移动。或者你知道如何刺激加速度计的运动,如http://www.scottlogic.com/blog/2014/03/19/simulator-enhancements.html – Ricky

+0

@Ricky:请避免用“Please upvote .. “。它只是增加了噪音。 – Matt