我已经将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);
答案 0 :(得分:3)
实施此委托方法:
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
基本上,一旦确定了用户的位置,MapView就会调用此位置。如果你动态检查 userLocation ,则无法保证。
您可以在 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.
}
}];
答案 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);
}