使用纬度和经度查找用户位置的名称

时间:2013-11-12 05:13:27

标签: ios geolocation mkmapview

我需要使用纬度和经度查找用户位置的名称。我使用以下代码使用注释来固定位置。

//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;

2 个答案:

答案 0 :(得分:0)

这可能是最简单的方式

- (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"]];
    }
  }];

}

答案 1 :(得分:0)

首先您需要导入 AddressBook / AddressBook.h 然后包括以下方法

-(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;
}
相关问题