iOS 实时定位功能
2015-07-30 17:50
507 查看
<pre name="code" class="objc">using namespace std; #import <CoreLocation/CoreLocation.h> #import <MapKit/MapKit.h> @class CLLocationManager; @class MKReverseGeocoder; @interface GpsInfo: NSObject<MKReverseGeocoderDelegate, CLLocationManagerDelegate> { @private CLLocationManager* locationMgr; MKReverseGeocoder* reverseGeocoder;// iso 5.0以下SDK版本使用 //CLGeocoder* clGeocoder;// iso 5.0及5.0以上SDK版本使用 } @property (nonatomic, retain) CLLocationManager* locationMgr; @property (nonatomic, retain) MKReverseGeocoder* reverseGeocoder; //@property (nonatomic, retain) CLGeocoder* clGeocoder;// iso 5.0及5.0以上SDK版本使用 // location detect - (void)UpdateLocationLatLng; @end
<pre name="code" class="objc">#import "GpsInfo.h"using namespace std;// this line is to fix "isinf" was not declared in this scop problem#import <MapKit/MapKit.h>#import <CoreLocation/CoreLocation.h>#import "Util.h"@implementation GpsInfo@synthesize locationMgr;@synthesize reverseGeocoder;//@synthesize clGeocoder;- (id)init{self = [super init];if (self) {locationMgr = nil;reverseGeocoder = nil;[self UpdateLocationLatLng];}return self;}- (void)dealloc {[locationMgr release];locationMgr = nil;[reverseGeocoder release]reverseGeocoder = nil;//[clGeocoder release];//clGeocoder = nil;[super dealloc];}-(void)UpdateLocationLatLng{if (locationMgr == nil){locationMgr = [[CLLocationManager alloc] init];locationMgr.delegate = self;locationMgr.desiredAccuracy = kCLLocationAccuracyBest;}[locationMgr startUpdatingLocation];}#pragma mark - get Placemark// iso 5.0以下SDK使用- (void)startedReverseGeoderWithLatitude: (double)latitude longitude: (double)longitude {CLLocationCoordinate2D coordinate2D;coordinate2D.latitude = latitude;coordinate2D.longitude = longitude;if (reverseGeocoder == nil){reverseGeocoder = [MKReverseGeocoder alloc];}reverseGeocoder = [reverseGeocoder initWithCoordinate:coordinate2D];reverseGeocoder.delegate = self;[reverseGeocoder start];}- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error{NSLog(@"Geocoder error!");}- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark{NSLog(@"GPS Info: country:%@, locality:%@, subLocality:%@, thoroughfare:%@, subThoroughfare:%@",placemark.country, placemark.locality, placemark.subLocality, placemark.thoroughfare, placemark.subThoroughfare);if (m_gpsInfoLocal != nil){if (placemark.country != nil){};else{};if (placemark.locality != nil){};else{};if (placemark.subLocality != nil){};else{};if (placemark.thoroughfare != nil){};else{};if (placemark.subThoroughfare != nil){};else{};}}/*! This project use DeploymentTarget for 4.3// iso 5.0及5.0以上SDK版本使用- (void)locationAddressWithCLLocation:(CLLocation*)locationGps{if (self.clGeocoder == nil)self.clGeocoder = [[CLGeocoder alloc] init];[self.clGeocoder reverseGeocodeLocation:locationGps completionHandler:^(NSArray* placemarks, NSError* error){MKPlacemark* placemark = [placemarks objectAtIndex:0];}];}*/#pragma mark - location Delegate- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{NSLog(@"Location error!");}// iso 5.0及5.0以下SDK使用-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{if (!newLocation) {[self locationManager:manager didFailWithError:NULL];return;}/*! signbit 需要引入库framework libSystem.B.dylib,暂时注掉。// horizontalAccuracy,用来得到水平上的精确度,它的大小就是定位精度的半径,单位为米。如果值为-1,则说明此定位不可信。if (signbit(newLocation.horizontalAccuracy)) {[self locationManager:manager didFailWithError:NULL];return;}*/[manager stopUpdatingLocation];// 用完就停,减少耗电NSLog(@"current location: %f, %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);// 解析坐标获得城市信息/*这个项目使用4.3的SDKif ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0) {[self locationAddressWithCLLocation:newLocation];} else {[self startedReverseGeoderWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];}*/[self startedReverseGeoderWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];}@end
相关文章推荐
- iOS 判断GPS是否开启
- ios 图片自动轮播
- IOS第四天(5:创建备份区按钮和判断结果)
- iOS开发中遇到的bug
- iOS真机上的EXC_ARM_DA_ALIGN问题
- [内存管理] 内存五大分区
- iOS开发使用Tab Bar Controller实现页面导航功能
- ios 开发小技巧
- iOS 生成静态库方法,合并静态库等详解
- 电脑管家Win10正式版检测BIOS不通过的解决方法
- iOS开发系列--音频播放、录音、视频播放、拍照、视频录制
- IOS 播放雪花的两种方式(代码方式)
- iOS duplicate symbol _OBJC_CLASS 错误处理方法
- iOS 应用名称的国际化图解
- 网易新闻iOS版使用的18个开源组件
- iOS-#ifdef DEBUG代码块介绍
- iOS-#ifdef DEBUG代码块介绍
- IOS实现点击软键盘的Next/Return按钮或者空白处后自动隐藏键盘
- ARC机制
- iOS中几种数据持久化方案:我要永远地记住你!