…………纠偏 篇…………..
1. 涉及接口:<CoreLocation/CoreLocation.h>
2. 核心代码解读:
if ([CLLocationManager locationServicesEnabled]) {
[self.locationManager setDelegate:self];
[self.locationManager setDistanceFilter:200];
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[self.locationManager startUpdatingLocation];
} // 开始定位
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation;
//此代理方法可以实现新位置更新,即一定距离(200) 则更新一次当前位置,保持经纬度始终为最新
self.coordinate = [newLocation coordinate];
self.coordinate.longitude,self.coordinate.latitude // 更新后的经纬度
TIPS:虽然上面方法中获取到了目前的经纬度,但是这个经纬度并不是真实的经纬度,而是火星经纬度,理所当然,下面就是考虑如何纠偏了,因为直接用这个经纬度去获取周边信息,基本不算定位,属于分身之术。
经过又一轮的查询相关资料和询问相关人士,居然小小纠偏却蕴藏很大商机,很多公司都有自己的一套纠偏方案,或者是购买第三方纠偏数据库,没有办法,只能再找,最后发现,我们的国产互联网巨头百度老大居然提供一个纠偏接口,真是感动啊,开动纠偏程序:
————————
NSString *rectificationURL=[NSString stringWithFormat:@"http://api.map.baidu.com/ag/coord/convert?from=0&to=2&x=%f&y=%f",self.coordinate.longitude,self.coordinate.latitude]; // 带上经纬度开始纠偏,对API有疑问,请自觉百度一下
纠偏结果如下:
{“error”:0,
“x”:”MTE2LjQzNTM1NDU0NjQ0″,
“y”:”MzkuOTEzNzAwOTAwNjA4″
} // 0 即为正确纠偏,后面的太不厚道了,居然用GTMBase64 加密,还好,费一番功夫下载Google 的”GTMBase64.h” 代码顺利解密,汗…
结果如下:
{
error: 0,
x: “MTE2LjQzNTM1NDU0NjQ0″, 116.43535454644
y: “MzkuOTEzNzAwOTAwNjA4″ 39.913700900608
}
再次感谢百度,接下来当然是带上经度,带上纬度,开始取周边地理信息,关键代码如下:
NSString *url = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&language=%@&sensor=false", aCoordinate.latitude, aCoordinate.longitude,NSLocalizedStringFromTable(@"signLanguage", @"sign", nil)]; //获取用户当前具体地理位置
NSString *url = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=%@&types=%@&sensor=true&key=%@&language=%@", aCoordinate.latitude, aCoordinate.longitude, [NSString stringWithFormat:@"%i", RADIUS], @”", kGOOGLE_API_KEY,NSLocalizedStringFromTable(@”signLanguage”, @”sign”, nil)]; //获取用户周边地理位置
Tips:若对上述API 有疑问,请自觉前往:https://developers.google.com/places/documentation/#Authentication 其中涉及的具体参数都有说明,请自动取舍,切记先申请 kGOOGLE_API_KEY.
引用地址:iOS地图定位纠偏
原文:http://www.cnblogs.com/On1Key/p/5234375.html