iOS 根据经纬度反查 地名
在iOS中
定位自己的当前位置,知道经纬度很简单,然后有些时候要知道地名,apple 也有了现成的api直接调用就可以(以下方法是iOS5.0以上的,现在基本都忽略了 iOS5.0以下的设备)
#pragma mark - #pragma mark CLLocationManagerDelegate - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { CLGeocoder *geocoder=[[CLGeocoder alloc] init]; [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) { CLPlacemark *placemark=[placemarks objectAtIndex:0]; NSLog(@"name:%@\n country:%@\n postalCode:%@\n ISOcountryCode:%@\n ocean:%@\n inlandWater:%@\n locality:%@\n subLocality:%@ \n administrativeArea:%@\n subAdministrativeArea:%@\n thoroughfare:%@\n subThoroughfare:%@\n", placemark.name, placemark.country, placemark.postalCode, placemark.ISOcountryCode, placemark.ocean, placemark.inlandWater, placemark.administrativeArea, placemark.subAdministrativeArea, placemark.locality, placemark.subLocality, placemark.thoroughfare, placemark.subThoroughfare); }]; if (wasFound) return; wasFound = YES; CLLocationCoordinate2D loc = [newLocation coordinate]; strLatitude = [NSString stringWithFormat: @"%f", loc.latitude]; strLongitude = [NSString stringWithFormat: @"%f", loc.longitude]; NSLog(@"strLatitude==%@,strLongitude==%@",strLatitude,strLongitude); }
log 是:
name:市民广场 country:中国 postalCode:(null) ISOcountryCode:CN ocean:(null) inlandWater:(null) locality:广东省 subLocality:(null) administrativeArea:深圳市 subAdministrativeArea:福田区 thoroughfare:市民广场 subThoroughfare:(null)
第二种方法就是 我们已经知道了一个经纬度 然后反查地名:
自己写个方法:
-(void)change{ //22.540681,=114.061324 CLLocationCoordinate2D coordinate; coordinate.latitude = 22.540681; coordinate.longitude = 114.061324; CLLocation *newLocation=[[CLLocation alloc]initWithLatitude:coordinate.latitude longitude: coordinate.longitude]; CLGeocoder *geocoder=[[CLGeocoder alloc] init]; [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) { CLPlacemark *placemark=[placemarks objectAtIndex:0]; NSLog(@"我我的:%@\n country:%@\n postalCode:%@\n ISOcountryCode:%@\n ocean:%@\n inlandWater:%@\n locality:%@\n subLocality:%@ \n administrativeArea:%@\n subAdministrativeArea:%@\n thoroughfare:%@\n subThoroughfare:%@\n", placemark.name, placemark.country, placemark.postalCode, placemark.ISOcountryCode, placemark.ocean, placemark.inlandWater, placemark.administrativeArea, placemark.subAdministrativeArea, placemark.locality, placemark.subLocality, placemark.thoroughfare, placemark.subThoroughfare); }]; }
nslog 的结果和上面的一样。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。