ios平台gps简单应用(获取经纬度、获取详细地址)
2013-01-30 12:22
549 查看
这个是不带view继承的使用方法,网上大部分都是在view的基础上使用。其实就是用NSObject代替相关view。
需要注意的是返回的placemark里有可能有null值的,使用之前要进行非空检测。
直接上代码:
//GpsInfo.h
//GpsInfo.mm
需要注意的是返回的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
相关文章推荐
- iOS之获取经纬度并通过反向地理编码获取详细地址
- iOS之获取经纬度并通过反向地理编码获取详细地址
- iOS之获取经纬度并通过反向地理编码获取详细地址
- Android Map Api 使用和开发(3)浮动搜索框 ,通过地址名称获取经纬度和详细地址并定位
- Android Google Maps API 网络服务用于网络定位、计算路线、获取经纬度、获取详细地址等
- 百度地图API简单应用——1.根据地址查询经纬度
- 百度地图API简单应用——1.根据地址查询经纬度
- Google Map通过经纬度获取详细地址
- 百度地图API简单应用——1.根据地址查询经纬度
- Android Map Api 使用和开发(3)浮动搜索框 ,通过地址名称获取经纬度和详细地址并定位
- 百度地图API简单应用——1.根据地址查询经纬度
- [iOS 获取AppStored 中应用的下载地址]
- iOS定位获取当前所在经纬度,进而获得具体地址(省市地区街道)
- iOS 高德地图设置中心点,获取中心点的详细位置和经纬度
- PHP CURL根据详细地址获取腾讯地图经纬度
- 转载 Android Map Api 使用和开发 定位我的位置、地图弹出泡泡、通过经纬度获取地址 浮动搜索框 ,通过地址名称获取经纬度和详细地址并定位
- android手机获取gps和基站的经纬度地址实现代码
- Unity ios平台GPS地址位置定位
- iOS定位获取当前所在经纬度,进而获得具体地址(省市地区街道)
- iOS 获取appStore的链接地址,从app中跳转 appStore中应用