如何从内容的总地址获取坐标?

时间:2012-03-29 10:17:33

标签: iphone ios

我正在获取位置地址的总地址和坐标,但我只需要在谷歌地图上显示的坐标。在iPhone中怎么可能?

- (void)geocode {
    [SVGeocoder geocode:addressField.text
             completion:^(NSArray *placemarks, NSError *error) {
                 UIAlertView *alertView;

                 if(!error && placemarks) {
                     SVPlacemark *placemark = [placemarks objectAtIndex:0];

                     NSLog(@"%@",placemarks);
                     alertView = [[UIAlertView alloc] initWithTitle:@"Placemark Found!" message:[placemark description] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                 } else {
                     alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:[error description] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                 }

                 [alertView show];
                 [alertView release];
             }];

地理编码的响应

  

“{\ n address = {\ n City = Nalgonda; \ n Country =   印度; \ n CountryCode = IN; \ n州=“Andhra   Pradesh \“; \ n}; \ n坐标= {\ n纬度=   \“17.05239 \”; \ n \ longitude = \“79.26718 \”; \ n}; \ n
  formattedAddress = \“Nalgonda,Andhra Pradesh,India \”; \ n}“

1 个答案:

答案 0 :(得分:3)

试试这个

- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address
{
   double latitude = 0, longitude = 0;
   NSString *esc_addr =  [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
   NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];
   NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
   if (result) {
        NSScanner *scanner = [NSScanner scannerWithString:result];
        if ([scanner scanUpToString:@"\"lat\":" intoString:nil] && [scanner scanString:@"\"lat\":" intoString:nil]) {
          [scanner scanDouble:&latitude];
          if ([scanner scanUpToString:@"\"lng\":" intoString:nil] && [scanner scanString:@"\"lng\":" intoString:nil]) {
              [scanner scanDouble:&longitude];
          }
        }
    }
   CLLocationCoordinate2D center;
   center.latitude = latitude;
   center.longitude = longitude;
   return center;
}