【问题标题】:How to get place name from lattitude and longitude?如何从纬度和经度中获取地名?
【发布时间】:2016-05-25 06:47:15
【问题描述】:

我从 api 获取纬度和经度,并且需要从这些坐标中获取地名,以便我使用这段代码

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)  

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

【问题讨论】:

  • 是的先生,我对这个概念有疑问
  • 是的,先生,我会读到它,但你现在能帮我吗
  • 从这里获取它@sandeep stackoverflow.com/questions/14346516/…
  • @sandeeptomar 请打印您的 LocationAtual 值。
  • @DharmbirSingh 这些是先生 28.566540 77.209841 错误我从 api 获取这些值

标签: ios objective-c google-geocoder


【解决方案1】:

viewDidLoadOfYour 类或任何其他类中添加:

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

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

【讨论】:

  • 是的,先生,这对我有用,感谢您了解我的新概念
【解决方案2】:

This Question 的 Ram G 答案中尝试此代码,它适用于您

- (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);

    }];

}

【讨论】:

  • 遇到同样的问题,先生
  • 此块未执行 [geocoder reverseGeocodeLocation:bestLocation completionHandler:^(NSArray *placemarks, NSError *error) { if (error){ NSLog(@"Geocode failed with error: %@", error) ;返回; } CLPlacemark *placemark = [placemarks objectAtIndex:0]; NSLog(@"placemark.ISOcountryCode %@",placemark.ISOcountryCode); NSLog(@"locality %@",placemark.locality); NSLog(@"邮政编码 %@",placemark.postalCode); }];
  • CLLocationManagerDelegate 在 .h 文件中声明并导入 #import
  • 导入这个#import
  • 你的纬度是否合适?
猜你喜欢
  • 2015-09-25
  • 1970-01-01
  • 2011-08-18
  • 1970-01-01
  • 2017-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多