1. 程式人生 > >iOS定位座標轉換工具

iOS定位座標轉換工具

如果你正需要這樣的工具,保證拿過去就能用
如果你還不知道這是什麼工具,我保證以後的iOS開發中肯定會用
如果你是iOS大牛,期待您能指點一下 ^_^

好了,廢話不多說了。上乾貨

座標系介紹

首先介紹一下目前的定位座標系統
1、地球座標 :( 代號:GPS、WGS84 )--- 有W就是世界通用的
也就是原始座標體系,這是國際公認的世界標準座標體系;

使用 WGS84 座標系統的產品有
蘋果的 CLLocationManager 獲取的座標

2、火星座標: (代號:GCJ-02)--- G國家 C測繪 J局 02年測繪的
為了保證國家安全,不被逮人獲知精準的地點,國內使用的一切座標,都必須是經過測繪局精密處理的座標。你要是不用?估計你也不能不用,呵呵

使用 GCJ-02 火星座標系統的產品有
高德地圖騰訊地圖阿里雲地圖靈圖51地圖

注意:現在蘋果系統自帶的地圖使用的是高德地圖,所以蘋果地帶的地圖應用,用的是GCJ-02的座標系統。但是程式碼中CLLocationManager獲取到的是WGS84座標系的座標

3、其他座標 :百度座標系統 (代號:BD-09)
大百度地圖的座標系統,豈能跟你們一樣。百度座標是在火星座標的基礎上再次加密計算而獲得的

使用 BD-09 座標系統的產品有
百度地圖

座標轉換

開發中你會遇到這樣的需求
1、通過當前和地址的經緯度,點選導航按鈕
2、如果手機裝有百度地圖,就跳轉百度地圖導航
3、如果沒有百度,有高德地圖,就跳轉到高德地圖導航
4、如果在沒有,就跳轉騰訊地圖導航
如果········來人,把產品經理拖出去,宰掉!!!

在開發過程中,通過 CLLocationManager 拿到地球座標座標。就可以在跳轉不同導航產品之前,進行座標轉換

地球座標 ---> 火星座標

- (NSDictionary *)locationMarsFromEarth_earthLat:(double)latitude earthLon:(double)longitude {
    // 首先判斷座標是否屬於天朝
    if (![self isInChinaWithlat:latitude lon:longitude]) {
        return @{@"latitude":@(latitude),
                 @"longitude":@(longitude)
                };
    }
    double a = 6378245.0;
    double ee = 0.00669342162296594323;

    double dLat = [self transform_earth_from_mars_lat_lat:(latitude - 35.0) lon:(longitude - 35.0)];
    double dLon = [self transform_earth_from_mars_lng_lat:(latitude - 35.0) lon:(longitude - 35.0)];
    double radLat = latitude / 180.0 * M_PI;
    double magic = sin(radLat);
    magic = 1 - ee * magic * magic;
    double sqrtMagic = sqrt(magic);
    dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * M_PI);
    dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * M_PI);

    double newLatitude = latitude + dLat;
    double newLongitude = longitude + dLon;
    NSDictionary *dic = @{@"latitude":@(newLatitude),
                          @"longitude":@(newLongitude)
                          };
    return dic;
}
- (BOOL)isInChinaWithlat:(double)lat lon:(double)lon {
    if (lon < 72.004 || lon > 137.8347)
        return NO;
    if (lat < 0.8293 || lat > 55.8271)
        return NO;
    return YES;
}
- (double)transform_earth_from_mars_lat_lat:(double)y lon:(double)x {
    double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(fabs(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) + 3320 * sin(y * M_PI / 30.0)) * 2.0 / 3.0;
    return ret;
}

- (double)transform_earth_from_mars_lng_lat:(double)y lon:(double)x {
    double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(fabs(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;
}

火星座標 <---> 百度座標

/** 百度座標 => 火星座標 */
- (NSDictionary *)marsLocationFromBaidu_baiduLat:(double)latitude baiduLon:(double)longitude {
    double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    double x = longitude - 0.0065, y = latitude - 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 newLatitude = z * sin(theta);
    double newLongitude = z * cos(theta);
    NSDictionary *dic = @{@"latitude":@(newLatitude),
                          @"longitude":@(newLongitude)
                          };
    return dic;
}


/** 火星座標 => 百度座標 */
- (NSDictionary *)baiduLocationFromMars_marsLat:(double)latitude marsLon:(double)longitude {
    double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    double x = longitude, y = latitude;
    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 newLatitude = z * sin(theta) + 0.006;
    double newLongitude = z * cos(theta) + 0.0065;
    NSDictionary *dic = @{@"latitude":@(newLatitude),
                          @"longitude":@(newLongitude)
                          };
    return dic;
}

相關推薦

iOS定位座標轉換工具

如果你正需要這樣的工具,保證拿過去就能用 如果你還不知道這是什麼工具,我保證以後的iOS開發中肯定會用 如果你是iOS大牛,期待您能指點一下 ^_^ 好了,廢話不多說了。上乾貨 座標系介紹 首先介紹一下目前的定位座標系統 1、地球座標 :( 代號:GPS、WGS84 )--- 有W就是世界通用的 也就是原始

iOS音訊格式轉換工具庫:ExtAudioConverter

前段時間的工作,和iOS的音訊相關,需要一個功能:將音訊從wav格式轉為mp3格式。這個需求不是很強,但卻不太好實現。 於是我寫了一個音訊格式轉換的開源庫,這個庫包含了iOS/OS X支援的所有音訊格式,另外也支援mp3格式(使用lame實現)。它仿照OS X

iOS座標轉換

// 將畫素point由point所在檢視轉換到目標檢視view中,返回在目標檢視view中的畫素值 - (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view; // 將畫素point從view中轉換到當前檢視中,返回在當前檢視中的畫素值

IOS火星座標轉換

<pre name="code" class="objc">#import <Foundation/Foundation.h> #import <CoreLocation/CoreLocation.h> @interface ZSLocationConverter :

ionic2 geolocation定位,將geolocation座標轉換為百度座標,高德地圖座標

安裝 geolocation 外掛 執行以下命令 npm install --save @ionic-native/geolocation 將geolocation外掛在app.module.ts內宣告 import { Geolocation } from '@ionic-nativ

城建座標與經緯度轉換工具

核心部分底層原始碼 CoodinateCover.java package PkgCover; import java.io.PrintStream; public class CoodinateCover { private GaussProjection _GP; priva

python+selenium利用線上程式碼轉換工具,批量定位百度頁面元素https://www.sojson.com/jshtml.html(附操作步驟)

一、定位一組元素如下圖 二、選擇copy element複製頁面元素 <div id="u1"><a href="http://news.baidu.com" name="tj_tr

iOS開發 關於UIView中的座標轉換

在開發中我們經常會需要判斷兩個控制元件是否包含重疊,此時如果控制元件A和B的座標原點如果不確定的話,那麼肯定會導致比較不正確發生錯誤 判斷包含重疊的程式碼如下: CGRectContainsRect(<#CGRect rect1#>, <#CGRe

IOS-- UIView中的座標轉換

// 將畫素point由point所在檢視轉換到目標檢視view中,返回在目標檢視view中的畫素值 - (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view; // 將畫素point從view中轉換到當前檢視中,返回在當前檢視中的畫素值

IOS/Swift UIView中的座標轉換

// 將畫素point由point所在檢視轉換到目標檢視view中,返回在目標檢視view中的畫素值 - (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view; // 將畫素point從view中轉換到當前檢視中,返回在當前檢視中的畫素值

ROS 主動蒙特卡羅粒子濾波定位演算法 AMCL 解析-- map與odom座標轉換的方法

ROS AMCL 演算法根據訂閱到的地圖資料配合鐳射掃描特徵,使用粒子濾波獲取最佳定位點,該點稱為Mp (point on map), 它是相對於地圖map上的座標,也就是base_link相對map上的座標。 odom 的原點是機器人啟動時刻的位置,它在map上的位置

C# UTM座標和WGS84座標轉換工具

工具根據:http://home.hiwaay.net/~taylorc/toolbox/geography/geoutm.html js程式碼改編 工具原始碼github:https://github.com/JeroLong/TUMAndWGS84TransTool.git 效果: 主要程式碼

office文檔、圖片、音/視頻格式轉換工具

ria 變量 ora lis window emoji 格式轉換 ffi stand 1、音頻/視屏轉換工具VLC https://wiki.videolan.org/Mp3/#Container_formats http://wenku.baidu.com/vie

iOS定位和獲取當前天氣

cat ide 獲取 data device targe user urn fig   這裏是定義了一個類,用來當app 啟動的時候,在後臺獲取當前和位置,並根據位置獲取當前天氣信息,當點擊底部的 tabbar 顯示我的控制器的時候,在頁面上顯示當前位置和當前的天氣狀況,天

iOS坐標轉換

rec elf self. ram nbsp 多行 view 返回 rect // 將像素point由point所在視圖轉換到目標視圖view中,返回在目標視圖view中的像素值 - (CGPoint)convertPoint:(CGPoint)point toVie

iOS 高效率編程工具

tinypng .com bsp targe 網站 .cn ref 編程 tar 原文鏈接: 確實有很多好用的工具。 http://huluo666.cn/2016/03/30/iOS%20高效率編程工具篇/ 圖片壓縮網站:https://tinypng.comiOS

iOS將漢字轉換為拼音

pen tin eat nsstring data ddc literal objective word 將漢字轉換為拼音 - (NSString *)chineseToPinyin:(NSString *)chinese withSpace:(BOO

GHO2VMDK轉換工具分享含VS2010源碼

change batch ogr combine 參數 false 失敗 rms windows 平常經常用到虛擬機,每次從gho轉換為vmdk時都要輸入cmd代碼,覺得麻煩,自己動手做了個gho2vmdk轉換工具,集成ghost32.exe文件,可以一鍵轉換,省時省事。運

使用Gson的Json轉換工具

space .com p s win left ebo href face get 382bt我S辭籃廝0綽yhttp://www.facebolw.com/space/2102274 u6靡q壬第62曳ihttp://www.facebolw.com/space/210

iOS 11 適配工具欄(UIToolbar)

con width cti ios 11 區域 rain 適配 itoo 有時 在iOS11中,ToolBar 裏面的按鈕的矩形可觸模區域非常小,緊貼著圖片,所有有時候點著沒反應 在 UIButton 創建的時候,同時加一句這樣的代碼: [[button.width