Чтение названия текущего местоположения с помощью карты в iPhone

Я читаю значения широты и долготы текущего местоположения, а затем успешно закрепляю это местоположение в iPhone. Теперь я хочу прочитать это название места, используя эти значения широты и долготы.

Я использовал следующий код для чтения текущего местоположения:

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

    CLLocation *whereIAm = userLocation.location;

    NSLog(@"I'm at %@", whereIAm.description);

    latitude = whereIAm.coordinate.latitude;
    longitude = whereIAm.coordinate.longitude;

    NSLog(@"LAT : %f",latitude);
    NSLog(@"LONG : %f",longitude);

    mapView.mapType=MKMapTypeStandard;
    [mapView setMapType:MKMapTypeStandard];
    [mapView setZoomEnabled:YES];
    [mapView setScrollEnabled:YES];

    MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } }; 
    region.center.latitude =latitude;  //22.569722 ;
    region.center.longitude = longitude;  //88.369722;
    region.span.longitudeDelta = 0.01f;
    region.span.latitudeDelta = 0.01f;
    [mapView setRegion:region animated:YES]; 

    [mapView setDelegate:self];

    DisplayMap *ann = [[DisplayMap alloc] init]; 
    ann.title = @" Welcome";
    ann.subtitle = @"Hi"; 
    ann.coordinate = region.center; 
   [mapView addAnnotation:ann];   
}


- (void)viewDidLoad {
      [super viewDidLoad];

      mapView.showsUserLocation = YES;
      mapView.delegate = self;
   }



-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation: (id<MKAnnotation>)annotation {
   MKPinAnnotationView *pinView = nil; 
   if(annotation != mapView.userLocation) 
   {
    static NSString *defaultPinID = @"com.invasivecode.pin";
    pinView = (MKPinAnnotationView *)[mapView       dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
   }     
   [mapView.userLocation setTitle:@"I am here"];
   return pinView;        
 }

person Velmurugan    schedule 29.09.2010    source источник
comment
Вам нужно попробовать отформатировать код еще раз   -  person willcodejavaforfood    schedule 29.09.2010


Ответы (1)


Используйте протокол MKReverseGeocoderDelegate следующим образом:

@interface YourViewController : UIViewController <MKReverseGeocoderDelegate> 
{
}


@implementation YourViewController

- (void)mapView:(MKMapView *)mapView1 didUpdateUserLocation:(MKUserLocation *)userLocation{
     CLLocation *whereIAm = userLocation.location;
     MKReverseGeocoder *reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:whereIAm.coordinate];
     reverseGeocoder.delegate = self;
     [reverseGeocoder start];
}

Теперь реализуйте следующий делегат метода, чтобы получить ответ:

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {
      NSLog(@"I'm at %@", placemark.locality);
}

Для получения дополнительной информации см. здесь: http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKReverseGeocoder_Class/

person MathieuF    schedule 29.09.2010
comment
Спасибо за ответ. Это очень полезно. Я закончил свой вопрос для вашего ответа. Спасибо. - person Velmurugan; 29.09.2010
comment
MKReverseGeocoder устарел с iOS 5.0, вам нужно CLGeocoder вместо него: developer.apple.com/library/prerelease/ios/documentation/ - person János; 20.01.2015