这个是不带view继承的使用方法,网上大部分都是在view的基础上使用。其实就是用NSObject代替相关view。
需要注意的是返回的placemark里有可能有null值的,使用之前要进行非空检测。
直接上代码:
//GpsInfo.h
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
//GpsInfo.mm
#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的SDK
if ([[[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