您的位置:首页 > 移动开发 > IOS开发

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 

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: