ios平台gps简单应用(获取经纬度、获取详细地址)

本文介绍了一个不依赖于视图控制器的位置服务实现方式,通过NSObject来管理CLLocationManager和MKReverseGeocoder,实现了获取地理位置信息的功能,并提供了详细的代码示例。

这个是不带view继承的使用方法,网上大部分都是在view的基础上使用。其实就是用NSObject代替相关view。

需要注意的是返回的placemark里有可能有null值的,使用之前要进行非空检测。

直接上代码:

//GpsInfo.h

[plain]  view plain copy
  1. using namespace std;  
  2. #import <CoreLocation/CoreLocation.h>  
  3. #import <MapKit/MapKit.h>  
  4.   
  5. @class CLLocationManager;  
  6. @class MKReverseGeocoder;  
  7. @interface GpsInfo: NSObject<MKReverseGeocoderDelegate, CLLocationManagerDelegate> {  
  8.   
  9. @private  
  10.     CLLocationManager* locationMgr;  
  11.     MKReverseGeocoder* reverseGeocoder;// iso 5.0以下SDK版本使用  
  12.     //CLGeocoder* clGeocoder;// iso 5.0及5.0以上SDK版本使用  
  13. }  
  14.   
  15. @property (nonatomic, retain) CLLocationManager* locationMgr;  
  16. @property (nonatomic, retain) MKReverseGeocoder* reverseGeocoder;  
  17. //@property (nonatomic, retain) CLGeocoder* clGeocoder;// iso 5.0及5.0以上SDK版本使用  
  18.   
  19.   
  20. // location detect  
  21. - (void)UpdateLocationLatLng;  
  22.   
  23. @end  

//GpsInfo.mm


[plain]  view plain copy
  1. #import "GpsInfo.h"  
  2. using namespace std;// this line is to fix "isinf" was not declared in this scop problem  
  3. #import <MapKit/MapKit.h>  
  4. #import <CoreLocation/CoreLocation.h>  
  5. #import "Util.h"  
  6. @implementation GpsInfo  
  7.   
  8.   
  9. @synthesize locationMgr;  
  10. @synthesize reverseGeocoder;  
  11. //@synthesize clGeocoder;  
  12.   
  13. - (id)init  
  14. {  
  15.     self = [super init];  
  16.     if (self) {  
  17.     locationMgr = nil;  
  18.         reverseGeocoder = nil;  
  19.         [self UpdateLocationLatLng];  
  20.     }  
  21.     return self;  
  22. }  
  23.   
  24. - (void)dealloc {  
  25.     [locationMgr release];  
  26.     locationMgr = nil;  
  27.     [reverseGeocoder release]  
  28.     reverseGeocoder = nil;  
  29.     //[clGeocoder release];  
  30.     //clGeocoder = nil;  
  31.     [super dealloc];  
  32. }  
  33.   
  34. -(void)UpdateLocationLatLng  
  35. {  
  36.     if (locationMgr == nil)  
  37.     {  
  38.         locationMgr = [[CLLocationManager alloc] init];  
  39.         locationMgr.delegate = self;  
  40.         locationMgr.desiredAccuracy = kCLLocationAccuracyBest;  
  41.     }  
  42.       
  43.     [locationMgr startUpdatingLocation];  
  44. }  
  45.   
  46. #pragma mark - get Placemark  
  47. // iso 5.0以下SDK使用  
  48. - (void)startedReverseGeoderWithLatitude: (double)latitude longitude: (double)longitude {  
  49.     CLLocationCoordinate2D coordinate2D;  
  50.     coordinate2D.latitude = latitude;  
  51.     coordinate2D.longitude = longitude;  
  52.       
  53.     if (reverseGeocoder == nil)  
  54.     {  
  55.         reverseGeocoder = [MKReverseGeocoder alloc];  
  56.     }  
  57.     reverseGeocoder = [reverseGeocoder initWithCoordinate:coordinate2D];  
  58.       
  59.       
  60.     reverseGeocoder.delegate = self;  
  61.     [reverseGeocoder start];  
  62. }  
  63.   
  64. - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error  
  65. {  
  66.     NSLog(@"Geocoder error!");  
  67. }  
  68.   
  69. - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark  
  70. {  
  71.     NSLog(@"GPS Info: country:%@, locality:%@, subLocality:%@, thoroughfare:%@, subThoroughfare:%@",  
  72.           placemark.country, placemark.locality, placemark.subLocality, placemark.thoroughfare, placemark.subThoroughfare);  
  73.       
  74.     if (m_gpsInfoLocal != nil)  
  75.     {  
  76.         if (placemark.country != nil)  
  77.             {};  
  78.         else  
  79.             {};  
  80.         if (placemark.locality != nil)  
  81.             {};  
  82.         else  
  83.             {};  
  84.         if (placemark.subLocality != nil)  
  85.             {};  
  86.         else  
  87.             {};  
  88.         if (placemark.thoroughfare != nil)  
  89.             {};  
  90.         else  
  91.             {};  
  92.         if (placemark.subThoroughfare != nil)  
  93.             {};  
  94.         else  
  95.             {};  
  96.     }  
  97. }  
  98. /*  
  99.  ! This project use DeploymentTarget for 4.3  
  100. // iso 5.0及5.0以上SDK版本使用  
  101. - (void)locationAddressWithCLLocation:(CLLocation*)locationGps  
  102. {  
  103.     if (self.clGeocoder == nil)  
  104.     self.clGeocoder = [[CLGeocoder alloc] init];  
  105.       
  106.     [self.clGeocoder reverseGeocodeLocation:locationGps completionHandler:^(NSArray* placemarks, NSError* error)  
  107.     {  
  108.         MKPlacemark* placemark = [placemarks objectAtIndex:0];  
  109.     }];  
  110. }  
  111. */  
  112. #pragma mark - location Delegate  
  113. - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error  
  114. {  
  115.     NSLog(@"Location error!");  
  116. }  
  117.   
  118. // iso 5.0及5.0以下SDK使用  
  119. -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation  
  120. {  
  121.     if (!newLocation) {  
  122.         [self locationManager:manager didFailWithError:NULL];  
  123.         return;  
  124.     }  
  125.     /*  
  126.      ! signbit 需要引入库framework libSystem.B.dylib,暂时注掉。  
  127.     // horizontalAccuracy,用来得到水平上的精确度,它的大小就是定位精度的半径,单位为米。如果值为-1,则说明此定位不可信。  
  128.     if (signbit(newLocation.horizontalAccuracy)) {  
  129.         [self locationManager:manager didFailWithError:NULL];  
  130.         return;  
  131.     }  
  132.      */  
  133.       
  134.     [manager stopUpdatingLocation];// 用完就停,减少耗电  
  135.       
  136.     NSLog(@"current location: %f, %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);  
  137.           
  138.     // 解析坐标获得城市信息  
  139.     /*这个项目使用4.3的SDK  
  140.     if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0) {  
  141.         [self locationAddressWithCLLocation:newLocation];  
  142.     } else {  
  143.         [self startedReverseGeoderWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];  
  144.     }  
  145.      */  
  146.     [self startedReverseGeoderWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];  
  147. }  
  148.   
  149.   
  150. @end  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值