2010-09-29 31 views
2

J'ai lu les valeurs de latitude et de longitude de l'emplacement actuel, puis j'ai réussi à identifier cet emplacement dans l'iPhone. Maintenant, je veux lire ce nom de lieu en utilisant ces valeurs de latitude et de longitude.Lecture du nom de l'emplacement actuel à l'aide de l'affichage de la carte dans l'iPhone

J'ai utilisé le code suivant pour lire le trouver l'emplacement actuel:

- (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;   
} 
+2

Vous devez essayer de formater à nouveau votre code – willcodejavaforfood

Répondre

5

Utilisez le protocole MKReverseGeocoderDelegate comme ceci:

@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]; 
} 

mettre en œuvre maintenant le délégué de méthode suivante pour obtenir la réponse:

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

Pour plus d'informations regardez ceci: http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKReverseGeocoder_Class/

+0

Merci pour votre réponse. C'est très utile. J'ai terminé mon numéro pour votre réponse. Merci. – Velmurugan

+1

'MKReverseGeocoder' obsolète depuis iOS 5.0 vous avez besoin de' CLGeocoder' à la place: https://developer.apple.com/library/prerelease/ios/documentation/CoreLocation/Reference/CLGeocoder_class/index.html –