如何在iPhone中获取当前位置纬度经度?

时间:2013-12-10 13:09:50

标签: ios iphone ipad core-location

我想让我当前的位置拉长,因为我已经写了一些代码,但我错了地方的lat很长 这是我的代码。

locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone; 
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[locationManager startUpdatingLocation];
float latitude = locationManager.location.coordinate.latitude;
float longitude = locationManager.location.coordinate.longitude;

2 个答案:

答案 0 :(得分:2)

要获取当前位置,请编写以下代码:

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate=self;
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
locationManager.distanceFilter=kCLDistanceFilterNone;

[locationManager startUpdatingLocation];

然后实现委托方法:

// CLLocationManagerDelegate

-(void)locationManager:(CLLocationManager *)manager
   didUpdateToLocation:(CLLocation *)newLocation
   fromLocation:(CLLocation *)oldLocation
{
    // Handle location updates
} 

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    // Handle error
}

答案 1 :(得分:0)

  1. 正确设置代理:[locationManager setDelegate:self]
  2. 实施正确的委托方法locationManager:didUpdateLocations:
  3. 读出位置数组。数组中的最后一项是最近找到的位置。