从iOS的经纬度获取位置名称
我想从经纬度中find当前的位置名称,
这是我尝试的代码片段,但我的日志显示空值除了在placemark
, placemark.ISOcountryCode
和placemark.country
我想要placemark.locality
和placemark.subLocality
值,但它显示空值。
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; // this creates the CCLocationManager that will find your current location locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; [locationManager startUpdatingLocation]; } // this delegate is called when the app successfully finds your current location - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { CLGeocoder *geocoder = [[CLGeocoder alloc] init]; [geocoder reverseGeocodeLocation:locationManager.location completionHandler:^(NSArray *placemarks, NSError *error) { NSLog(@"reverseGeocodeLocation:completionHandler: Completion Handler called!"); if (error){ NSLog(@"Geocode failed with error: %@", error); return; } NSLog(@"placemarks=%@",[placemarks objectAtIndex:0]); CLPlacemark *placemark = [placemarks objectAtIndex:0]; NSLog(@"placemark.ISOcountryCode =%@",placemark.ISOcountryCode); NSLog(@"placemark.country =%@",placemark.country); NSLog(@"placemark.postalCode =%@",placemark.postalCode); NSLog(@"placemark.administrativeArea =%@",placemark.administrativeArea); NSLog(@"placemark.locality =%@",placemark.locality); NSLog(@"placemark.subLocality =%@",placemark.subLocality); NSLog(@"placemark.subThoroughfare =%@",placemark.subThoroughfare); }]; } // this delegate method is called if an error occurs in locating your current location - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { NSLog(@"locationManager:%@ didFailWithError:%@", manager, error); }
提前致谢。
编辑:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { CLGeocoder *geocoder = [[CLGeocoder alloc] init]; CLLocation *currentLocation = newLocation; if (currentLocation != nil) NSLog(@"longitude = %.8f\nlatitude = %.8f", currentLocation.coordinate.longitude,currentLocation.coordinate.latitude); // stop updating location in order to save battery power [locationManager stopUpdatingLocation]; [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) { NSLog(@"Found placemarks: %@, error: %@", placemarks, error); if (error == nil && [placemarks count] > 0) { CLPlacemark *placemark = [placemarks lastObject]; // strAdd -> take bydefault value nil NSString *strAdd = nil; if ([placemark.subThoroughfare length] != 0) strAdd = placemark.subThoroughfare; if ([placemark.thoroughfare length] != 0) { // strAdd -> store value of current location if ([strAdd length] != 0) strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark thoroughfare]]; else { // strAdd -> store only this value,which is not null strAdd = placemark.thoroughfare; } } if ([placemark.postalCode length] != 0) { if ([strAdd length] != 0) strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark postalCode]]; else strAdd = placemark.postalCode; } if ([placemark.locality length] != 0) { if ([strAdd length] != 0) strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark locality]]; else strAdd = placemark.locality; } if ([placemark.administrativeArea length] != 0) { if ([strAdd length] != 0) strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark administrativeArea]]; else strAdd = placemark.administrativeArea; } if ([placemark.country length] != 0) { if ([strAdd length] != 0) strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark country]]; else strAdd = placemark.country; } } }]; }
我正在给你用来解决地址的片段。 我还包括在必要的地方评论,以了解你的代码。 除此之外,如果你不理解任何东西的话,可以随便问任何来自snippet的问题。
在didUpdateToLocation
方法中写下面的代码片段
NSLog(@"didUpdateToLocation: %@", newLocation); CLLocation *currentLocation = newLocation; if (currentLocation != nil) NSLog(@"longitude = %.8f\nlatitude = %.8f", currentLocation.coordinate.longitude,currentLocation.coordinate.latitude); // stop updating location in order to save battery power [locationManager stopUpdatingLocation]; // Reverse Geocoding NSLog(@"Resolving the Address"); // “reverseGeocodeLocation” method to translate the locate data into a human-readable address. // The reason for using "completionHandler" ---- // Instead of using delegate to provide feedback, the CLGeocoder uses “block” to deal with the response. By using block, you do not need to write a separate method. Just provide the code inline to execute after the geocoding call completes. [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) { NSLog(@"Found placemarks: %@, error: %@", placemarks, error); if (error == nil && [placemarks count] > 0) { placemark = [placemarks lastObject]; // strAdd -> take bydefault value nil NSString *strAdd = nil; if ([placemark.subThoroughfare length] != 0) strAdd = placemark.subThoroughfare; if ([placemark.thoroughfare length] != 0) { // strAdd -> store value of current location if ([strAdd length] != 0) strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark thoroughfare]]; else { // strAdd -> store only this value,which is not null strAdd = placemark.thoroughfare; } } if ([placemark.postalCode length] != 0) { if ([strAdd length] != 0) strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark postalCode]]; else strAdd = placemark.postalCode; } if ([placemark.locality length] != 0) { if ([strAdd length] != 0) strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark locality]]; else strAdd = placemark.locality; } if ([placemark.administrativeArea length] != 0) { if ([strAdd length] != 0) strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark administrativeArea]]; else strAdd = placemark.administrativeArea; } if ([placemark.country length] != 0) { if ([strAdd length] != 0) strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark country]]; else strAdd = placemark.country; }
strAdd将使用地理位置返回地址..
享受编程!
带完成块的方法:
typedef void(^addressCompletion)(NSString *); -(void)getAddressFromLocation:(CLLocation *)location complationBlock:(addressCompletion)completionBlock { __block CLPlacemark* placemark; __block NSString *address = nil; CLGeocoder* geocoder = [CLGeocoder new]; [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) { if (error == nil && [placemarks count] > 0) { placemark = [placemarks lastObject]; address = [NSString stringWithFormat:@"%@, %@ %@", placemark.name, placemark.postalCode, placemark.locality]; completionBlock(address); } }]; }
这是如何使用它:
CLLocation* eventLocation = [[CLLocation alloc] initWithLatitude:_latitude longitude:_longitude]; [self getAddressFromLocation:eventLocation complationBlock:^(NSString * address) { if(address) { _address = address; } }];
使用google api进行反向地理标记:
url: http : //maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true_or_false
来自: https : //developers.google.com/maps/documentation/geocoding/
我在Swift 3中实现了Niru的解决scheme,如果有人需要它,
let geocoder = CLGeocoder() geocoder.reverseGeocodeLocation(self.location!) { (placemarksArray, error) in if (placemarksArray?.count)! > 0 { let placemark = placemarksArray?.first let number = placemark!.subThoroughfare let bairro = placemark!.subLocality let street = placemark!.thoroughfare self.addressLabel.text = "\(street!), \(number!) - \(bairro!)" } }
如果您正在尝试获取的位置在此列表中列出,那么您的代码中出现错误。
http://developer.apple.com/library/ios/#technotes/tn2289/_index.html#//apple_ref/doc/uid/DTS40011305
否则CLGeoCoder不具有其他国家的地址。
我面临同样的问题,所以我用这个来获得地址..给出了一个非常准确的地址。
http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true_or_false
首先,筛选didUpdateToLocation中的位置,以防止使用caching或错误的地理位置进行地理编码
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow]; if (abs(locationAge) > 5.0) return; if (newLocation.horizontalAccuracy < 0) return;
此外,尝试将reverseGeoCoding方法从didUpdateToLocation移开
一旦你有了latitude
,你可以在这里使用CLGeocoder的位置的longitude
值是一个教程 ,可以帮助你。
使用这个API获取数据并传递经纬度值。
用于地理位置的API
尝试这个
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) { //..NSLog(@"Found placemarks: %@, error: %@", placemarks, error); if (error == nil && [placemarks count] > 0) { placemark = [placemarks lastObject]; lblgetaddrees.text = [NSString stringWithFormat:@"%@,%@,%@,%@,%@,%@", placemark.subThoroughfare, placemark.thoroughfare, placemark.postalCode, placemark.locality, placemark.administrativeArea, placemark.country]; } else { //..NSLog(@"%@", error.debugDescription); } } ];