簡體   English   中英

如何從iOS中的mapKit獲取經度和緯度?

[英]How to get latitude , longitude from mapKit in iOS?

我已經將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);

實現此委托方法:

- (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

用這個:

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

另一種方法是通過提供地址作為文本。

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

-(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);


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM