如何从iOS中的mapKit获取纬度,经度?

时间:2012-06-26 14:41:43

标签: ios frameworks mapkit

我已经将mapkit框架集成到我的项目中,一切正常。我只是在地图中显示用户当前的位置,如下所示:

mapView.delegate = self;
    mapView.showsUserLocation = YES;
    MKUserLocation *userLocation = mapView.userLocation;
    //numbers show map zoom. 500,500 = map stretches 500 meters to the North and the South of current location
    MKCoordinateRegion region =
    MKCoordinateRegionMakeWithDistance (userLocation.location.coordinate,1000,1000);
    [mapView setRegion:region animated:NO];

问题是我需要将2个变量存储到当前位置的纬度和经度但这些值存储在哪里?

我试图打印下面但是它给了我0.000。

NSLog(@"Latitude: %f" , mapView.userLocation.coordinate.latitude);
NSLog(@"Longitude: %f" , mapView.userLocation.coordinate.latitude);

4 个答案:

答案 0 :(得分:3)

实施此委托方法:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation

基本上,一旦确定了用户的位置,MapView就会调用此位置。如果你动态检查 userLocation ,则无法保证。

您可以在 MKMapViewDelegate协议参考

中找到其余的委托方法

http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapViewDelegate_Protocol/MKMapViewDelegate/MKMapViewDelegate.html#//apple_ref/occ/intf/MKMapViewDelegate

答案 1 :(得分:2)

使用它:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    NSLog(@"Latitude: %f" , userLocation.coordinate.latitude);
    NSLog(@"Longitude: %f" , userLocation.coordinate.latitude);
}

答案 2 :(得分:0)

另一种方法是将地址作为文本提供。

geocoder = [[CLGeocoder alloc] init];    

[geocoder geocodeAddressString:@"1 Infinite Loop" 
  completionHandler:^(NSArray* placemarks, NSError* error){
 for (CLPlacemark* aPlacemark in placemarks)
 {
     // Process the placemark.
 }
}];

其解释如下:https://developer.apple.com/library/ios/documentation/userexperience/conceptual/LocationAwarenessPG/UsingGeocoders/UsingGeocoders.html

答案 3 :(得分:0)

-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    CLLocation *location = locations.lastObject;
    NSLog(@"Latitude: %f" , self.mapview.userLocation.coordinate.latitude);
    NSLog(@"Longitude: %f" , self.mapview.userLocation.coordinate.longitude);


}