-3

I need to find the name of the user's location using latitude and longitude. The following code I have used to pin the point in location using annotation.

//MAP VIEW Point

MKCoordinateRegion myRegion;

//Center
CLLocationCoordinate2D center;
center.latitude=latitude;
center.longitude=longitude;

//Span
MKCoordinateSpan span;
span.latitudeDelta=THE_SPAN;
span.longitudeDelta=THE_SPAN;

myRegion.center=center;
myRegion.span=span;

//Set our mapView
[MapViewC setRegion:myRegion animated:YES];

//Annotation

//1.create coordinate for use with the annotation
CLLocationCoordinate2D wimbLocation;
wimbLocation.latitude=latitude;
wimbLocation.longitude=longitude;

Annotation * myAnnotation= [Annotation alloc];
myAnnotation.coordinate=wimbLocation;
jscs
  • 63,694
  • 13
  • 151
  • 195
user2756361
  • 41
  • 1
  • 1
  • 6

2 Answers2

0

This could be the simplest way

- (void)reverseGeocodeLocation {
      CLLocation *someLocation=[[CLLocation alloc]initWithLatitude:20.256456 longitude:68.545656]
  CLGeocoder *geocoder = [[CLGeocoder alloc] init];
  [geocoder reverseGeocodeLocation:someLocation completionHandler:^(NSArray *placemarks, NSError *error) {
    if(placemarks.count){
      NSDictionary *dictionary = [[placemarks objectAtIndex:0] addressDictionary];
      [self.addressOutlet setText:[dictionary valueForKey:@"Street"]];
      [self.cityOutlet setText:[dictionary valueForKey:@"City"]];
      [self.stateOutlet setText:[dictionary valueForKey:@"State"]];
      [self.zipOutlet setText:[dictionary valueForKey:@"ZIP"]];
    }
  }];

}
Jay Gajjar
  • 2,661
  • 2
  • 21
  • 35
0

First You need to import the AddressBook/AddressBook.h Then include the below method

-(NSString*)locationFromCoordinate:(CLLocationCoordinate2D)coordinate
{
     CLGeocoder *geocoder = [[CLGeocoder alloc] init];

     CLLocation *loc = [[CLLocation alloc]initWithLatitude:coordinate.latitude
                                                    longitude:coordinate.longitude];
     NSString *address;
     [geocoder reverseGeocodeLocation:loc
               completionHandler:^(NSArray *placemarks, NSError *error) {

                   if (error) {
                       NSLog(@"Failed with error: %@", error);
                       return;
                   }

                   if (placemarks.count > 0)
                   {
                       CLPlacemark *placemark = placemarks[0];

                       NSDictionary *addressDictionary =
                       placemark.addressDictionary;

                       address = [addressDictionary
                                            objectForKey:(NSString *)kABPersonAddressStreetKey];

                   }

      return address;
}
manujmv
  • 6,450
  • 1
  • 22
  • 35