我使用下面的代碼來獲得一個按鈕緯度和經度0稱爲Objective-C的第二次
-(CLLocationCoordinate2D) getLocation{
CLLocationCoordinate2D coordinate;
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
if ([CLLocationManager locationServicesEnabled]) {
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
coordinate = [location coordinate];
}
else {
coordinate.latitude = 0.0;
coordinate.longitude = 0.0;
}
return coordinate;
}
當點擊第一次按鈕的點擊座標時,我得到我的但如果我再次單擊該按鈕,經度和緯度值爲0.0000 任何建議
當你在XCode中進行調試時會發生什麼?是否因爲[CLLocationManager locationServicesEnabled]計算結果爲'false'而調用'else'子句?還是因爲'[位置座標]'返回'0,0'?當你從'[locationManager location]'返回時,你確定'location'不是null嗎? – selbie
此外,文檔暗示您應該僅爲整個應用程序實例化一個CLLocationManager實例。您似乎在每次點擊按鈕時都會創建一個新的。 – selbie
我確認if條件滿足調試期間.. – user1966460