如何在iOS 4.0/4.2中获取当前位置的经纬度?

5

我在iOS 4.0和4.2中都无法获取当前位置的经纬度,实际上我想在我的应用程序中从当前位置到特定位置绘制路线,我在我的应用程序中尝试了许多方法,但是无法得到结果。

如果有人知道,请回复我。

谢谢! :)


1
你尝试过使用 CLLocationManager 吗? - EmptyStack
3个回答

16
CLLocationManager *lm = [[CLLocationManager alloc] init];
lm.delegate = self;
lm.desiredAccuracy = kCLLocationAccuracyBest;
lm.distanceFilter = kCLDistanceFilterNone; 
[lm startUpdatingLocation];

CLLocation *location = [lm location];

CLLocationCoordinate2D coord;
coord.longitude = location.coordinate.longitude;
coord.latitude = location.coordinate.latitude;
// or a one shot fill
coord = [location coordinate];

1
如果您使用此方法,location 很可能是 nil 或过时的。建议采用 locationManager:didUpdateToLocation:fromLocation: 方法实现来接收位置更新。 - Mark Adams
1
这是正确的。如果他们想要最准确的机会,他们应该从该方法内查询坐标。但这种方式至少可以给他们一个良好的开端。 - Bill Burgess
嗨@BillBurgess,这个答案在IOS 6上对我有用,但在IOS 5.1上不起作用...有什么替代方案吗...! - Karthik
1
可能在过去两年中发生了改变,但现在应该是:coord.longitude = location.coordinate.longitude; coord.latitude = location.coordinate.latitude; - cloudsurfin
1
这应该被接受为正确答案,它对我来说是有效的(至少)。 - higgs241

5

下载这个示例

该示例将显示您当前的经纬度。


0

您需要调用CLLocationManager(请参见Apple文档)。- startUpdatingLocation和- stopUpdatingLocation是您将要使用的主要方法之一。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接