【问题标题】:Find the name of the user's location using latitude and longitude [duplicate]使用纬度和经度查找用户位置的名称[重复]
【发布时间】:2013-11-12 05:13:27
【问题描述】:

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

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

【问题讨论】:

标签: ios geolocation mkmapview


【解决方案1】:

这可能是最简单的方法

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

}

【讨论】:

【解决方案2】:

首先你需要导入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;
}

【讨论】:

    猜你喜欢
    • 2017-11-10
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    • 2017-08-01
    • 2018-01-08
    • 1970-01-01
    • 2011-01-16
    • 1970-01-01
    相关资源
    最近更新 更多