iOS定位坐标转换工具-b
坐标转换
一、坐标简介
目前国内主流坐标系类型主要有三种:WGS84、GCJ02、BD09;
1.WGS84:为一种大地坐标系,也是目前广泛使用的GPS全球卫星定位系统使用的坐标系,如:iOS系统自带地图坐标。
- GCJ02:是由中国国家测绘局制订的地理信息系统的坐标系统,是由WGS84坐标系经加密后的坐标系,如:高德地图、google地图、soso地图、aliyun地图、mapabc地图和amap地图所用坐标
- BD09:百度坐标系,在GCJ02坐标系基础上再次加密。其中BD09ll表示百度经纬度坐标,BD09mc表示百度墨卡托米制坐标,如:百度地图所用坐标
二、一般项目集成的sdk都支持别的坐标转成自己的坐标,如百度地图支持WGS84和GCJ02转BD09ll,也支持BD09ll转GCJ02,但是,下面就主要介绍一下GCJ02和BD09ll转WGS84
二、BD09ll转GCJ02
/**
百度坐标转高德坐标
@param glat 纬度
@param glon 经度
@return 数组【转换后的纬度,转换后的经度】
*/
+ (NSArray *)bd09llToGCJLat:(double)glat lon:(double)glon {
double X_PI = M_PI * 3000.0 / 180.0;
double x = glon - 0.0065;
double y = glat - 0.006;
double z = sqrt(x * x + y * y) - 0.00002 * sin(y * X_PI);
double theta = atan2(y, x) - 0.000003 * cos(x * X_PI);
double lat = z * sin(theta);
double lon = z * cos(theta);
NSArray *latlon = @[[NSNumber numberWithDouble:lat],[NSNumber numberWithDouble:lon]];
return latlon;
}
三、GCJ02转WGS84(只支持国内)
/**
高德坐标转系统自带坐标
@param array 数组 需要转换的坐标【纬度,经度】
@return 数组【转换后的纬度,转换后的经度】
*/
+ (NSArray *)gcjToWGS:(NSArray *)array {
double lat = [array.firstObject doubleValue];
double lon = [array.lastObject doubleValue];
if ([self outOfChinalat:lat lon:lon]){
return @[[NSNumber numberWithDouble:lat],[NSNumber numberWithDouble:lon]];
}
NSArray *latlon = [self deltalat:lat lon:lon];
return latlon;
}
+ (BOOL)outOfChinalat:(double)lat lon:(double)lon {
if (lon < 72.004 || lon > 137.8347)
return true;
if (lat < 0.8293 || lat > 55.8271)
return true;
return false;
}
+ (NSArray *)deltalat:(double)wgLat lon:(double)wgLon {
double OFFSET = 0.00669342162296594323;
double AXIS = 6378245.0;
double dLat = [self transformLatx:(wgLon - 105.0) lon:(wgLat - 35.0)];
double dLon = [self transformLonx:(wgLon - 105.0) lon:(wgLat - 35.0)];
double radLat = wgLat / 180.0 * M_PI;
double magic = sin(radLat);
magic = 1 - OFFSET * magic * magic;
double sqrtMagic = sqrt(magic);
dLat = (dLat * 180.0) / ((AXIS * (1 - OFFSET)) / (magic * sqrtMagic) * M_PI);
dLon = (dLon * 180.0) / (AXIS / sqrtMagic * cos(radLat) * M_PI);
return @[[NSNumber numberWithDouble:(wgLat - dLat)],[NSNumber numberWithDouble:(wgLon - dLon)]];
}
+ (double)transformLatx:(double)x lon:(double)y {
double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(abs(x));
ret += (20.0 * sin(6.0 * x * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0;
ret += (20.0 * sin(y * M_PI) + 40.0 * sin(y / 3.0 * M_PI)) * 2.0 / 3.0;
ret += (160.0 * sin(y / 12.0 * M_PI) + 320 * sin(y * M_PI / 30.0)) * 2.0 / 3.0;
return ret;
}
+ (double)transformLonx:(double)x lon:(double)y {
double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(abs(x));
ret += (20.0 * sin(6.0 * x * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0;
ret += (20.0 * sin(x * M_PI) + 40.0 * sin(x / 3.0 * M_PI)) * 2.0 / 3.0;
ret += (150.0 * sin(x / 12.0 * M_PI) + 300.0 * sin(x / 30.0 * M_PI)) * 2.0 / 3.0;
return ret;
}
四、BD09ll转WGS84,目前的做法是先将BD09ll坐标转成GCJ02坐标,然后再转成WGS84坐标。
五、经实测,转换后的坐标,另GCJ02坐标转WGS84坐标有一个更精确的算法,不过里面设计到了递归。
//手机自带地图
//当前位置
MKMapItem *mylocation = [MKMapItemmapItemForCurrentLocation];
//前面填写纬度
CLLocationCoordinate2D coords2 =CLLocationCoordinate2DMake(cell.model.coordinate.latitude, cell.model.coordinate.longitude);
//目的地的位置
MKMapItem *toLocation = [[MKMapItemalloc] initWithPlacemark:[[MKPlacemarkalloc] initWithCoordinate:coords2addressDictionary:nil]];
toLocation.name =cell.model.title;
NSArray *items = [NSArrayarrayWithObjects:mylocation, toLocation,nil];
NSDictionary *options =@{ MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsMapTypeKey: [NSNumbernumberWithInteger:MKMapTypeStandard],MKLaunchOptionsShowsTrafficKey:@YES};
//打开苹果自身地图应用,并呈现特定的item
[MKMapItemopenMapsWithItems:items launchOptions:options];
//百度地图
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {//判断是否安装了百度地图APP
NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/marker?location=%f,%f&title=%@&content=%@&src=webapp.marker.yourCompanyName.yourAppName",cell.model.coordinate.latitude,cell.model.coordinate.longitude,cell.stroeNameLab.text,cell.stroeAddressLab.text] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}else{}
//高德地图
NSString *urlString = [[NSStringstringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%f&lon=%f&dev=0&style=2",cell.model.title,@"baidumap",cell.model.coordinate.latitude, cell.model.coordinate.longitude]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:urlString]];
----------------------
//跳到地图导航
- ( void ) btnPermanentAddressClick
{
NSString * appName = @ "自己的app名字" ;
NSString * urlScheme = @ "自己的urlScheme" ;
UIAlertController * alert = [ UIAlertController alertControllerWithTitle : @ "选择地图" message : nil preferredStyle : UIAlertControllerStyleActionSheet ] ;
UIAlertAction * appleMap = [ UIAlertAction actionWithTitle : @ "苹果地图" style : UIAlertActionStyleDefault handler : ^ ( UIAlertAction * action ) {
if ( [ [ UIApplication sharedApplication ] canOpenURL : [ NSURL URLWithString : @ "baidumap://" ] ] )
{
//地理编码器
CLGeocoder * geocoder = [ [ CLGeocoder alloc ] init ] ;
[ geocoder geocodeAddressString : @ "上海嘉定区伊宁路2000号" completionHandler : ^ ( NSArray < CLPlacemark * > * _Nullable placemarks , NSError * _Nullable error ) {
for ( CLPlacemark * placemark in placemarks ) {
//坐标(经纬度)
CLLocationCoordinate2D coordinate = placemark . location . coordinate ;
MKMapItem * currentLocation = [ MKMapItem mapItemForCurrentLocation ] ;
MKMapItem * toLocation = [ [ MKMapItem alloc ] initWithPlacemark : [ [ MKPlacemark alloc ] initWithCoordinate : coordinate addressDictionary : nil ] ] ;
[ MKMapItem openMapsWithItems : @ [ currentLocation , toLocation ]
launchOptions : @ { MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving ,
MKLaunchOptionsShowsTrafficKey : [ NSNumber numberWithBool : YES ] } ] ;
}
} ] ;
} else {
[ NXPopAlertViewLabel showMessage : @ "您的手机没有安装苹果地图" ] ;
}
} ] ;
UIAlertAction * baiduMap = [ UIAlertAction actionWithTitle : @ "百度地图" style : UIAlertActionStyleDefault handler : ^ ( UIAlertAction * action ) {
if ( [ [ UIApplication sharedApplication ] canOpenURL : [ NSURL URLWithString : @ "baidumap://" ] ] )
{
//地理编码器
CLGeocoder * geocoder = [ [ CLGeocoder alloc ] init ] ;
[ geocoder geocodeAddressString : @ "上海嘉定区伊宁路2000号" completionHandler : ^ ( NSArray < CLPlacemark * > * _Nullable placemarks , NSError * _Nullable error ) {
for ( CLPlacemark * placemark in placemarks ) {
//坐标(经纬度)
CLLocationCoordinate2D coordinate = placemark . location . coordinate ;
NSString * urlString = [ [ NSString stringWithFormat : @ "baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=目的地&mode=driving&coord_type=gcj02" , coordinate . latitude , coordinate . longitude ] stringByAddingPercentEncodingWithAllowedCharacters : [ NSCharacterSet URLQueryAllowedCharacterSet ] ] ;
[ [ UIApplication sharedApplication ] openURL : [ NSURL URLWithString : urlString ] options : @ { } completionHandler : nil ] ;
}
} ] ;
} else {
[ NXPopAlertViewLabel showMessage : @ "您的手机没有安装百度地图" ] ;
}
} ] ;
UIAlertAction * gaodeMap = [ UIAlertAction actionWithTitle : @ "高德地图" style : UIAlertActionStyleDefault handler : ^ ( UIAlertAction * action ) {
if ( [ [ UIApplication sharedApplication ] canOpenURL : [ NSURL URLWithString : @ "iosamap://" ] ] )
{
//地理编码器
CLGeocoder * geocoder = [ [ CLGeocoder alloc ] init ] ;
[ geocoder geocodeAddressString : @ "上海嘉定区伊宁路2000号" completionHandler : ^ ( NSArray < CLPlacemark * > * _Nullable placemarks , NSError * _Nullable error ) {
for ( CLPlacemark * placemark in placemarks ) {
//坐标(经纬度)
CLLocationCoordinate2D coordinate = placemark . location . coordinate ;
NSString * urlString = [ [ NSString stringWithFormat : @ "iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%f&lon=%f&dev=0&style=2" , appName , urlScheme , coordinate . latitude , coordinate . longitude ] stringByAddingPercentEncodingWithAllowedCharacters : [ NSCharacterSet URLQueryAllowedCharacterSet ] ] ;
[ [ UIApplication sharedApplication ] openURL : [ NSURL URLWithString : urlString ] options : @ { } completionHandler : nil ] ;
}
} ] ;
} else {
[ NXPopAlertViewLabel showMessage : @ "您的手机没有安装高德地图" ] ;
}
} ] ;
UIAlertAction * gugeMap = [ UIAlertAction actionWithTitle : @ "谷歌地图" style : UIAlertActionStyleDefault handler : ^ ( UIAlertAction * action ) {
if ( [ [ UIApplication sharedApplication ] canOpenURL : [ NSURL URLWithString : @ "comgooglemaps://" ] ] )
{
//地理编码器
CLGeocoder * geocoder = [ [ CLGeocoder alloc ] init ] ;
[ geocoder geocodeAddressString : @ "上海嘉定区伊宁路2000号" completionHandler : ^ ( NSArray < CLPlacemark * > * _Nullable placemarks , NSError * _Nullable error ) {
for ( CLPlacemark * placemark in placemarks ) {
//坐标(经纬度)
CLLocationCoordinate2D coordinate = placemark . location . coordinate ;
NSString * urlString = [ [ NSString stringWithFormat : @ "comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving" , appName , urlScheme , coordinate . latitude , coordinate . longitude ] stringByAddingPercentEncodingWithAllowedCharacters : [ NSCharacterSet URLQueryAllowedCharacterSet ] ] ;
[ [ UIApplication sharedApplication ] openURL : [ NSURL URLWithString : urlString ] options : @ { } completionHandler : nil ] ;
}
} ] ;
} else {
[ NXPopAlertViewLabel showMessage : @ "您的手机没有安装谷歌地图" ] ;
}
} ] ;
UIAlertAction * cancel = [ UIAlertAction actionWithTitle : @ "取消" style : UIAlertActionStyleDefault handler : ^ ( UIAlertAction * action ) {
} ] ;
[ alert addAction : appleMap ] ;
[ alert addAction : baiduMap ] ;
[ alert addAction : gaodeMap ] ;
[ alert addAction : gugeMap ] ;
[ alert addAction : cancel ] ;
[ self presentViewController : alert animated : YES completion : ^ {
} ] ;
}
参考链接
https://www.cnblogs.com/huahua0809/p/5235670.html
https://blog.csdn.net/autom_lishun/article/details/54092420
https://www.jianshu.com/p/b7fa7253bc1b
https://www.jianshu.com/p/ee83aed42d6d
https://www.jianshu.com/p/27dbe24e8771
相关开放平台入口
百度开放平台 http://lbsyun.baidu.com
百度地图 ios 快速入口 http://lbsyun.baidu.com/index.php?title=iossdk/guide/navigation/allocation
https://lbsyun.baidu.com/index.php?title=uri/api/ios
高德地图 ios 快速入口 https://lbs.amap.com/api/amap-mobile/guide/ios/navi
iOS定位坐标转换工具-b的更多相关文章
- ios 定位
ios 定位新功能----在程序中实现定位功能 Core Location是iOS SDK中一个提供设备位置的框架.可以使用三种技术来获取位置:GPS.蜂窝或WiFi.在这些技术中,GPS最为精准,如 ...
- iOS第三方库管理工具
作者:彷徨iOS 原文地址1:http://iostree.sinaapp.com/?p=78 原文地址2:http://blog.csdn.net/wzzvictory/article/detail ...
- IOS定位服务的应用
IOS定位服务的应用 一.授权的申请与设置 二.定位服务相关方法 三.定位服务代理的相关方法 四.定位服务获取到的位置对象 五.航标定位得到的航标信息对象 IOS定位服务的应用 一.授权的申请与设置 ...
- Appium+Python3+iOS定位元素
前言: 最近在做IOS自动化测试,IOS的Appium环境都配置OK,执行起来真的慢,慢到怀疑人生,那么今天就来总结一下IOS定位方式和各个定位方式的速度排序. 据我观察,按查找元素的顺序速度,从快到 ...
- Android & iOS 启动画面工具
感谢Aone!为我们开发了如此便捷的工具!! 以下为原文: Android & iOS 启动画面工具 下载:OneSplash.启动画面工具.Aone.20190318.zip 说明:这一个 ...
- iOS定位原理和使用建议(转)
原文:http://ibbs.91.com/thread-1548870-1-1.html 看到很多网友讨论iOS设备定位的问题,这里将我们所了解的关于iPhone.iPad.iPod等的定位原理做详 ...
- IOS定位核心与地图
IOS定位核心与地图 Core Location以及Map框架包通常能给我们的应用程序添加定位和地图相关的服务.Core Location框架包通常是使用硬件设备来进行 ...
- Ios国际化翻译工具
IOS Translation Tool(IOS国际化翻译工具) 介绍 当IOS项目国际化的时候,手工去翻译每一个字符串是一件非常痛苦的事情.尤其是当项目中存在N多种语言.而且又很难保证,手工翻译的准 ...
- [转] Google 开源 iOS 应用测试工具:EarlGrey
Google 开源 iOS 应用测试工具:EarlGrey oschina 发布于: 2016年02月18日 (3评) 分享到: 收藏 +53 3月19日,深圳源创会火热报名中,go>&g ...
随机推荐
- Commons JXPath - Modifying Object Graphs
JXPath 除了可以 XPath 语法访问 JavaBeans.DOM/JDOM,也可以对其属性赋值. 以下面的 JavaBeans 为例. package com.huey.jxpath; imp ...
- Spring(3.2.3) - Beans(9): @Resoure & @Autowired
@Resource 和 @Autowired 都是用来装配依赖的,它们之间有些异同. @Resoure @Resource 是 JSR-250 规范的注解. @Resource 可以标注在字段.方法上 ...
- python学习day4--python基础--购物小程序
'''购物小程序:用户启动时先输入工资用户启动程序后打印商品列表允许用户选择购买商品允许用户不断购买各种商品购买时检测余额是否够,如果够直接扣款,否则打印余额不足允许用户主动退出程序,退出时打印已购商 ...
- 修改msconfig->引导->高级选项-》最大内存为512M
本来想开机提速的!手贱 把 最大内存设置成了512M 结果开机悲剧了,启用了微软的自动修复也不能解决问题!最后是WIN7 PE系统下直接修复boot结果了.遇到这种问题的朋友们可以试试喔
- WinForm 实现登录,验证成功,关闭登录界面,显示主界面
点击登录按钮时: ") { this.DialogResult = DialogResult.OK; this.Close(); } else { MessageBox.Show(" ...
- JavaScript学习笔记(13)——BOM
1.window 所有浏览器都支持window对象,它表示浏览器窗口本身. 所有 JavaScript 全局对象.函数以及变量均自动成为 window 对象的成员. 全局变量是 window 对象的属 ...
- FAILURE: Build failed with an exception. Crunching Cruncher screen.png failed
自己测试ionic的模板项目cutePuppyPics时,按照https://github.com/driftyco/ionic/blob/2.0/CHANGELOG.md#angular-updat ...
- sqlplus 可以登录 plsql 不能登录
最开始我以为是system用户被锁定了,但是解锁后仍然不可以登录.大神指导之后可以了,说是缺少监听器,解决过程如下: 1.将“tnsnames.ora”和“listener.ora”两个文件里的“lo ...
- 服务器发布WebService返回DataTable
初始化Datatable时,需要为Datatable命名.否则在客户端使用时,会报“datatable不能序列化...”导致表格无法从服务器端读取到. 例如: 服务器端: DataTable dt = ...
- lex&yacc8--wehter use in C++
bintree.h:12:1: error: unknown type name ‘using’ using namespace std; ============== bintree.h:28:1: ...