2014-05-20 38 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