2011-08-01 160 views

回答

13

实施CLLocationManagerDelegate写此功能,在你的viewController的viewDidLoad方法调用它。

-(void)startLocationManager 
{ 
    CLLocationManager *locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate=self; 
    locationManager.desiredAccuracy=kCLLocationAccuracyBestForNavigation; 
    [locationManager startUpdatingLocation]; 
} 

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{  
    CLLocationCoordinate2D coordinate = [newLocation coordinate]; 
    double dblLatitude = coordinate.latitude; 
    double dblLongitude = coordinate.longitude;  
} 
+1

非常感谢您在iOS 6中使用 –

+0

didUpdateToLocation已弃用,因此请使用** didUpdateLocations ** – swiftBoy

4

如果您使用CLLocationmanager,请确保您检查是否首先启用位置服务。我想这样做是为了获得当前位置:

if ([CLLocationManager locationServicesEnabled]) 
{ 
    CLLocationManager *locationManager = [[CLLocationManager alloc] init]; 
    [locationManager startUpdatingLocation]; 

    CLLocationCoordinate2D *currentLocation = locationManager.location; 

    [locationManager stopUpdatingLocation]; 
    [locationManager release]; 
} 

如果您正在使用的MapView,您可以用获取当前位置:

[mapView setShowsUserLocation:YES]; 
CLLocationCoordinate2D *currentLocation = mapView.userLocation.coordinate;