如何从经纬度获取地名?

时间:2016-05-25 06:47:15

标签: ios objective-c google-geocoder

我从api获得经度和纬度,需要从这些cooridinates获取地名,以便我使用这段代码

CLGeocoder *ceo = [[CLGeocoder alloc]init];

CLLocation *LocationAtual=[[CLLocation alloc] initWithLatitude:[[latlong objectForKey:@"latitude"] doubleValue] 
                                                     longitude:[[latlong objectForKey:@"longitude"] doubleValue]];

NSLog(@"loc %@", LocationAtual);

[ceo reverseGeocodeLocation:LocationAtual  completionHandler:^(NSArray *placemarks, NSError *error)
{
  CLPlacemark *placemark = [placemarks objectAtIndex:0];
  NSLog(@"placemark %@",placemark);
  //String to hold address
  NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
  NSLog(@"addressDictionary %@", placemark.addressDictionary);

  NSLog(@"placemark %@",placemark.region);
  NSLog(@"placemark %@",placemark.country);  // Give Country Name
  NSLog(@"placemark %@",placemark.locality); // Extract the city name
  NSLog(@"location %@",placemark.name);
  NSLog(@"location %@",placemark.ocean);
  NSLog(@"location %@",placemark.postalCode);
  NSLog(@"location %@",placemark.subLocality);

  NSLog(@"location %@",placemark.location);

  NSLog(@"I am currently at %@",locatedAt);
  NSLog(@"  ");
}
];

但当我的控制权在这一行上时

 [ceo reverseGeocodeLocation:LocationAtual  completionHandler:^(NSArray *placemarks, NSError *error)  

此代码块未执行。控制权。

2 个答案:

答案 0 :(得分:1)

viewDidLoad你的班级或任何其他班级中,只需添加:

- (void)viewDidLoad
{
    [super viewDidLoad];
// Add observer
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotPlaceInfo:) name:@"gotPlaceInfoNotification" object:nil];
}

//define `gotPlaceInfo`:
-(void)gotPlaceInfo:(NSNotification *)notif{

NSLog(@"place mark dict is  %@", notif.userInfo);
}

我刚在代码中添加了一行:

    CLGeocoder *ceo = [[CLGeocoder alloc]init];

    CLLocation *LocationAtual=[[CLLocation alloc] initWithLatitude:[[latlong objectForKey:@"latitude"] doubleValue] 
                                                         longitude:[[latlong objectForKey:@"longitude"] doubleValue]];

    NSLog(@"loc %@", LocationAtual);

    [ceo reverseGeocodeLocation:LocationAtual  completionHandler:^(NSArray *placemarks, NSError *error)
    {
      CLPlacemark *placemark = [placemarks objectAtIndex:0];

NSDictionary *currentLocationDictionary = @{@"CLPlacemark":CLPlacemark};
      NSLog(@"placemark %@",placemark);
      //String to hold address
      NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
      NSLog(@"addressDictionary %@", placemark.addressDictionary);

      NSLog(@"placemark %@",placemark.region);
      NSLog(@"placemark %@",placemark.country);  // Give Country Name
      NSLog(@"placemark %@",placemark.locality); // Extract the city name
      NSLog(@"location %@",placemark.name);
      NSLog(@"location %@",placemark.ocean);
      NSLog(@"location %@",placemark.postalCode);
      NSLog(@"location %@",placemark.subLocality);

      NSLog(@"location %@",placemark.location);

      NSLog(@"I am currently at %@",locatedAt);
      NSLog(@"  ");
NSNotification *notification2 = [NSNotification notificationWithName:@"gotPlaceInfoNotification" object:self userInfo:currentLocationDictionary];
    [[NSNotificationCenter defaultCenter] postNotification:notification2];
    }
    ];

希望它会给你一个全面的学习

答案 1 :(得分:0)

尝试使用Ram G的答案,This Question它适用于您

- (void) getAddressFromLatLon:(CLLocation *)bestLocation
{
    NSLog(@"%f %f", bestLocation.coordinate.latitude, bestLocation.coordinate.longitude);
    CLGeocoder *geocoder = [[CLGeocoder alloc] init] ;
    [geocoder reverseGeocodeLocation:bestLocation
                   completionHandler:^(NSArray *placemarks, NSError *error)
    {
        if (error){
            NSLog(@"Geocode failed with error: %@", error);
            return;
        }
        CLPlacemark *placemark = [placemarks objectAtIndex:0];
        NSLog(@"placemark.ISOcountryCode %@",placemark.ISOcountryCode);
        NSLog(@"locality %@",placemark.locality);
        NSLog(@"postalCode %@",placemark.postalCode);

    }];

}