1

我已将 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

4 回答 4

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

于 2012-06-26T14:47:17.523 回答
2

用这个:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    NSLog(@"Latitude: %f" , userLocation.coordinate.latitude);
    NSLog(@"Longitude: %f" , userLocation.coordinate.latitude);
}
于 2012-06-26T14:54:26.590 回答
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

于 2014-07-05T08:02:35.847 回答
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);


}
于 2016-07-25T06:11:37.217 回答