IOS通过经纬度进行反向地址解析(有些方法虽然过时了,但是依然很好用!)

 

IOS通过经纬度反向解析地址 

[cpp]  view plain copy
  1. <span style="font-family: Arial, Verdana, sans-serif; white-space: normal; background-color: rgb(255, 255, 255); ">1. 在工程里面引入CoreLocation.framework和MapKit.framework。</span>  

2. 在.h文件里面加入如下代码:

[cpp]  view plain copy
  1. #import <CoreLocation/CoreLocation.h>  
  2. #import <MapKit/MKReverseGeocoder.h>  
  3. #import <MapKit/MKPlacemark.h>  

[cpp]  view plain copy
  1. @interface RootViewController : UICustomViewController<CLLocationManagerDelegate, MKReverseGeocoderDelegate> {  
  2.     CLLocationManager *gps;  
  3. }  


3. 在.m文件中加入以下代码:

[cpp]  view plain copy
  1. - (void)locationManager:(CLLocationManager *)locationManager didUpdateToLocation:(CLLocation *)newLocation  
  2.            fromLocation:(CLLocation *) oldLocation;  
  3. {  
  4.     self.location = [NSString stringWithFormat:@"%f,%f",newLocation.coordinate.latitude, newLocation.coordinate.longitude];  
  5.     [self startedReverseGeoderWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];  
  6. }  
  7.   
  8. - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {  
  9.     if ( [error code] == kCLErrorDenied ) {  
  10.         [manager stopUpdatingHeading];  
  11.     } else if ([error code] == kCLErrorHeadingFailure) {  
  12.           
  13.     }  
  14. }  
  15.   
  16. -(void) startedReverseGeoderWithLatitude:(double)latitude longitude:(double)longitude{  
  17.     CLLocationCoordinate2D coordinate2D;  
  18.     coordinate2D.longitude = longitude;  
  19.     coordinate2D.latitude = latitude;  
  20.     MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:coordinate2D];  
  21.     geoCoder.delegate = self;  
  22.     [geoCoder start];  
  23. }  
  24.   
  25. -(void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark  
  26. {  
  27.       
  28.     NSString *subthroung=placemark.thoroughfare;  
  29.     NSString *local=placemark.locality;  
  30.     self.textFieldName.text = [NSString stringWithFormat:@"您当前所在位置:%@,%@",local, subthroung];  
  31.       
  32. }  
  33. -(void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error  
  34. {  
  35. }  

[cpp]  view plain copy
  1. - (void)viewDidLoad {  
  2.     [super viewDidLoad];  
  3.       
  4.     gps = [[CLLocationManager alloc] init];  
  5.     gps.delegate = self;  
  6.     gps.desiredAccuracy = kCLLocationAccuracyBest;  
  7.     gps.distanceFilter = kCLDistanceFilterNone;  
  8.     [gps startUpdatingLocation];  
  9. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值