版權(quán)聲明:本文為博主原創(chuàng)文章测萎,未經(jīng)博主允許不得轉(zhuǎn)載。
正在進(jìn)行的項(xiàng)目中有這樣的需求:定位獲得當(dāng)前經(jīng)緯度,再用百度Place API使用經(jīng)緯度查詢周邊信息。這里不需要顯示地圖,只需要定位跌穗。看似思路很順暢虏辫,做起來(lái)卻不容易蚌吸。
iPhone的GPS定位(CLLocationManager)獲得的經(jīng)緯坐標(biāo)是基于WGS-84坐標(biāo)系(世界標(biāo)準(zhǔn)),Google地圖使用的是GCJ-02坐標(biāo)系(中國(guó)特色的火星坐標(biāo)系)砌庄,這就是為什么獲得的經(jīng)緯坐標(biāo)在google地圖上會(huì)發(fā)生偏移羹唠。我項(xiàng)目需求是使用百度Place API,百度的經(jīng)緯坐標(biāo)在GCJ-02的基礎(chǔ)上再做了次加密娄昆,就是DB-09坐標(biāo)系佩微。就想使用百度地圖的[iOS](http://lib.csdn.net/base/ios) SDK,里面的坐標(biāo)系統(tǒng)都是一致的而不用轉(zhuǎn)換萌焰,由于不想讓項(xiàng)目太大哺眯,所以沒(méi)有用百度的sdk,所以另辟蹊徑了扒俯。
在網(wǎng)上搜索一番奶卓,有現(xiàn)成百度的接口轉(zhuǎn)換坐標(biāo),經(jīng)試驗(yàn) 從WGS-84到GCJ-02撼玄,再到DB-09夺姑,經(jīng)兩次轉(zhuǎn)換后,順利獲得當(dāng)前正確地理位置信息和周邊信息掌猛,當(dāng)然這些信息是來(lái)自百度的盏浙。
ZYLocationManager.h
[objc] view plain copy
import <Foundation/Foundation.h>
import <CoreLocation/CoreLocation.h>
import "Singleton.h"
typedef void(^locationBlock)(CLLocationCoordinate2D coor);
typedef void(^addressBlock)(NSString *address);
@interface ZYLocationManager : NSObject
singleton_interface(ZYLocationManager)
/**
- 獲取糾偏后的經(jīng)緯度(百度地圖經(jīng)緯度)
*/
- (void) getLocationCoordinate:(locationBlock) locaiontBlock;
/**
- 獲取糾偏后的經(jīng)緯度(百度地圖經(jīng)緯度)和地址
*/
- (void) getLocationCoordinate:(locationBlock) locaiontBlock address:(addressBlock) addressBlock;
@end
ZYLocationManager.m
[objc] view plain copy
import "ZYLocationManager.h"
import "AFNetworking.h"
define IOS_Version [[UIDevice currentDevice].systemVersion floatValue]
@interface ZYLocationManager ()<CLLocationManagerDelegate>{
// 保存block
locationBlock _locationBlock;
addressBlock _addressBlock;
}
@property (nonatomic, strong) CLLocationManager *lm;
@end
@implementation ZYLocationManager
singleton_implementation(ZYLocationManager)
/**
- 懶加載
*/
- (CLLocationManager *)lm
{
if (!_lm) {
_lm = [[CLLocationManager alloc] init];
_lm.delegate = self;
// 定位精準(zhǔn)度
_lm.desiredAccuracy = kCLLocationAccuracyBest;
// 重新定位的距離
_lm.distanceFilter = 1000.0f;
}
return _lm;
}
/**
- 類第一次使用的時(shí)候被調(diào)用
*/
- (void)initialize
{
ZYLocationManager *manager = [self sharedZYLocationManager];
// ios8后需要向用戶請(qǐng)求權(quán)限
if (IOS_Version >= 8.0) {
[manager.lm requestWhenInUseAuthorization];
[manager.lm requestAlwaysAuthorization];
}
// 開始定位
[manager.lm startUpdatingLocation];
}
pragma mark - CLLocationManager獲取經(jīng)緯度的代理方法
-
(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [locations lastObject];
CLLocationCoordinate2D coor = location.coordinate;
// NSLog(@"緯度:%.6f 經(jīng)度%.6f", coor.latitude, coor.longitude);
NSString *x1 = [NSString stringWithFormat:@"%f", coor.longitude];
NSString *y1 = [NSString stringWithFormat:@"%f", coor.latitude];
// http://api.map.baidu.com/ag/coord/convert?from=0&to=2&x=113.377346&y=23.132648
__block NSDictionary *dict1 = @{@"from":@"0",
@"to":@"2",
@"x":x1,
@"y":y1
};
AFHTTPRequestOperationManager *roManager = [AFHTTPRequestOperationManager manager];
// 1、ios系統(tǒng)經(jīng)緯度(國(guó)際標(biāo)準(zhǔn))轉(zhuǎn)谷歌經(jīng)緯度
[roManager GET:@"http://api.map.baidu.com/ag/coord/convert" parameters:dict1 success:^(AFHTTPRequestOperation *operation, id responseObject) {
__block NSString *resultX = [self base64Decode:responseObject[@"x"]];
__block NSString *resultY = [self base64Decode:responseObject[@"y"]];
dict1 = @{@"from":@"2",
@"to":@"4",
@"x":resultX,
@"y":resultY
};// 2荔茬、谷歌經(jīng)緯度轉(zhuǎn)百度經(jīng)緯度 [roManager GET:@"http://api.map.baidu.com/ag/coord/convert" parameters:dict1 success:^(AFHTTPRequestOperation *operation, id responseObject) { resultX = [self base64Decode:responseObject[@"x"]]; resultY = [self base64Decode:responseObject[@"y"]]; CLLocationCoordinate2D resultCoor = CLLocationCoordinate2DMake([resultY floatValue], [resultX floatValue]); // 給block賦值 if (_locationBlock) { _locationBlock(resultCoor); } [self getAddressWithCoordinate:resultCoor]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"%@", error); }];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
}];
// 停止定位
[self.lm stopUpdatingLocation];
} (void)getLocationCoordinate:(locationBlock)locaiontBlock
{
_locationBlock = locaiontBlock;
}(void)getLocationCoordinate:(locationBlock)locaiontBlock address:(addressBlock)addressBlock
{
_locationBlock = locaiontBlock;
_addressBlock = addressBlock;
}
pragma mark - base64解密
- (NSString *)base64Decode:(NSString *)str
{
// 1废膘、加密字符串轉(zhuǎn)二進(jìn)制數(shù)據(jù)
NSData *data = [[NSData alloc] initWithBase64EncodedString:str options:NSDataBase64DecodingIgnoreUnknownCharacters];
// 2、二進(jìn)制數(shù)據(jù)轉(zhuǎn)字符串
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
pragma mark - 經(jīng)緯度轉(zhuǎn)地址
-
(void)getAddressWithCoordinate:(CLLocationCoordinate2D)coor
{
if (coor.latitude == 0 || coor.longitude == 0) return;CLLocation *loca = [[CLLocation alloc] initWithLatitude:coor.latitude longitude:coor.longitude];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:loca completionHandler:^(NSArray *placemarks, NSError *error) {
if (placemarks.count == 0 || error) return;CLPlacemark *pm = [placemarks lastObject]; if (_addressBlock) { _addressBlock(pm.thoroughfare); }
}];
}
@end
IOS8后慕蔚,請(qǐng)求定位需要請(qǐng)求權(quán)限丐黄,代碼ZYLocationManager.m已經(jīng)寫好了,不過(guò)還需要在info.plist中坊萝,添加兩個(gè)屬性NSLocationAlwaysUsageDescription和NSLocationWhenInUseUsageDescription孵稽,屬性值即是你需要提示給用戶的信息,如下圖所示:將ZYLocationManager.h和ZYLocationManager.m拖入項(xiàng)目中十偶,即可直接調(diào)用ZYLocationManager.h定義的兩個(gè)方法菩鲜,獲取到百度經(jīng)緯度和地址。
原文出處: http://blog.csdn.net/u013454067/article/details/45131387