編輯:關於 Android 程式設計
一、先理清坐標體系
做定位相關的開發,坐標系這件事必須先搞明白,否則地圖上的點會莫名其妙偏出幾百米。常見的分類有三種:
1、WGS84,也常被叫作 GPS 坐標,是國際公認的世界標準坐標體系,手機 GPS 芯片直接吐出的就是它;
2、GCJ-02,俗稱「火星坐標」,由國家測繪局在 2002 年發布。在國內做地圖,基本都得用這一套,Google 地圖、高德、騰訊地圖走的都是它;
3、其他加密坐標,一般由 GCJ-02 再做一次偏移算法得出,比如百度的 BD-09、搜狗自家的坐標體系。
所以問題本質很清楚:GPS 拿到的是 WGS84,高德要的是 GCJ-02,中間必須做一次轉換。
二、兩種轉換方式
1. 直接調用高德 SDK
public AMapLocation fromGpsToAmap(Location location) { AMapLocation aMapLocation = new AMapLocation(location); CoordinateConverter converter = new CoordinateConverter(mContext); converter.from(CoordinateConverter.CoordType.GPS); try { converter.coord(new DPoint(location.getLatitude(), location.getLongitude())); DPoint desLatLng = converter.convert(); aMapLocation.setLatitude(desLatLng.getLatitude()); aMapLocation.setLongitude(desLatLng.getLongitude()); } catch (Exception e) { e.printStackTrace(); } return aMapLocation; } 這是官方推薦做法,代碼量最小。不過我在項目裡實際用它的時候踩了坑:引入新版本的高德定位 jar 之後,編譯階段友盟一直報包衝突,翻了一圈也沒定位到衝突點;把友盟統計的引用去掉,編譯立刻就正常了。統計又必須保留,最後只能放棄新版本的定位包,退回到老版本,上面這段代碼也就用不上了。
2. 自己動手轉換
如果因為依賴衝突等原因不方便引入新 SDK,也可以自己實現轉換算法。網上有一篇講得比較透徹的文章:https://www.eoeandroid.com/forum.php?mod=viewthread&tid=332419,思路是把 WGS84 加上一組非線性偏移量得到 GCJ-02,代碼如下。
public AMapLocation fromGpsToAmap(Location location) { LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); latLng = CoordinateUtil.transformFromWGSToGCJ(latLng); AMapLocation aMapLocation = new AMapLocation(location); aMapLocation.setLatitude(latLng.latitude); aMapLocation.setLongitude(latLng.longitude); return aMapLocation; } CoordinateUtil.java
public class CoordinateUtil { private static double a = 6378245.0; private static double ee = 0.00669342162296594323; /** * 手機GPS坐標轉火星坐標 * * @param wgLoc * @return */ public static LatLng transformFromWGSToGCJ(LatLng wgLoc) { //如果在國外,則默認不進行轉換 if (outOfChina(wgLoc.latitude, wgLoc.longitude)) { return new LatLng(wgLoc.latitude, wgLoc.longitude); } double dLat = transformLat(wgLoc.longitude - 105.0, wgLoc.latitude - 35.0); double dLon = transformLon(wgLoc.longitude - 105.0, wgLoc.latitude - 35.0); double radLat = wgLoc.latitude / 180.0 * Math.PI; double magic = Math.sin(radLat); magic = 1 - ee * magic * magic; double sqrtMagic = Math.sqrt(magic); dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * Math.PI); dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * Math.PI); return new LatLng(wgLoc.latitude + dLat, wgLoc.longitude + dLon); } public static double transformLat(double x, double y) { double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(x > 0 ? x : -x); ret += (20.0 * Math.sin(6.0 * x * Math.PI) + 20.0 * Math.sin(2.0 * x * Math.PI)) * 2.0 / 3.0; ret += (20.0 * Math.sin(y * Math.PI) + 40.0 * Math.sin(y / 3.0 * Math.PI)) * 2.0 / 3.0; ret += (160.0 * Math.sin(y / 12.0 * Math.PI) + 320 * Math.sin(y * Math.PI / 30.0)) * 2.0 / 3.0; return ret; } public static double transformLon(double x, double y) { double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(x > 0 ? x : -x); ret += (20.0 * Math.sin(6.0 * x * Math.PI) + 20.0 * Math.sin(2.0 * x * Math.PI)) * 2.0 / 3.0; ret += (20.0 * Math.sin(x * Math.PI) + 40.0 * Math.sin(x / 3.0 * Math.PI)) * 2.0 / 3.0; ret += (150.0 * Math.sin(x / 12.0 * Math.PI) + 300.0 * Math.sin(x / 30.0 * Math.PI)) * 2.0 / 3.0; return ret; } public static boolean outOfChina(double lat, double lon) { if (lon < 72.004 || lon > 137.8347) return true; if (lat < 0.8293 || lat > 55.8271) return true; return false; } } 這份實現裡有兩個細節值得注意:outOfChina 用經緯度大致框出國境範圍,國外坐標直接原樣返回不做偏移;常數 a 和 ee 分別是克拉索夫斯基椭球長半軸與第一偏心率平方,是這套算法固定使用的參數,不要隨意改動。
總結
兩種方案各有適用場景:新項目、依賴可控時優先用官方 SDK,準確省心;老項目遇到 jar 衝突,或者只想做一個坐標顯示、對精度要求不算苛刻,自己轉換也完全够用。以上就是本文的全部內容,希望對大家的學習或工作有所幫助,有疑問歡迎留言交流。
Android 內存洩露的自動化測試實踐
進行 Android 內存洩露分析時,面對成千上萬個對象,你是否感到頭疼?作為測試人員,在做完洩露測試後,是否有勇氣告訴開發同事「程序已無洩漏、可以放心發布」?內存洩露測
Shiro 回顧筆記:用 ini 檔動手驗證授權機制
一、Shiro 的授權是怎麼跑的 授權流程可以概括成一句話:先問「你是誰」,再問「你能做什麼」。而跟認證環節類似,Shiro 在判定權限時,最後仍然是回到 Realm 去
IntentService 源碼解析:從使用到原理
IntentService(Service)的使用場景 IntentService 與 Service 的區別 IntentService 的使用 IntentServi
一步步打造通用 Adapter:告別重複的 getView 樣板程式碼
ListView 大概是 Android 裡最常被使用的控制元件,而只要用到它,就躲不開 Adapter。我們習慣性地為每個列表寫一個 Adapter,然後重寫 getC