Android中GPS坐標轉(zhuǎn)換為高德地圖坐標詳解
一、坐標分類
地圖坐標大致分為幾種:
1、GPS、WGS84,也就是原始坐標體系,這是國際公認的世界標準坐標體系;
2、GCJ-02,又稱為“火星坐標”,國家測繪局在02年發(fā)布的坐標體系,在國內(nèi),至少得使用此坐標體系,比如:google、高德、騰訊地圖等;
3、其他特殊坐標體系,一般都是由火星坐標通過偏移算法計算得出的,比如百度使用的是BD-09坐標,搜狗使用的是自己的搜狗坐標。
二、坐標轉(zhuǎn)換
1、使用高德地圖sdk轉(zhuǎn)換
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包后,編譯的時候友盟總是提示我有包沖突,但是經(jīng)歷無數(shù)的尋找,都沒找出沖突的地方,當我把友盟統(tǒng)計的包引用去掉,編譯正常與行了。這里我被友盟坑了,但是必須要保留友盟統(tǒng)計。我只能放棄新的定位包,使用老版本的,也就不能用上面這個方式了。
2、自己轉(zhuǎn)換
通過在網(wǎng)上的搜索,找到一篇文章http://www.eoeandroid.com/forum.php?mod=viewthread&tid=332419,能很好的解決我的問題,也就是我們自己轉(zhuǎn)換坐標,方法如下。
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坐標轉(zhuǎn)火星坐標
*
* @param wgLoc
* @return
*/
public static LatLng transformFromWGSToGCJ(LatLng wgLoc) {
//如果在國外,則默認不進行轉(zhuǎn)換
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;
}
}
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
- Android 仿高德地圖可拉伸的BottomSheet的示例代碼
- Android基于高德地圖完全自定義Marker的實現(xiàn)方法
- Android調(diào)用高德地圖定位的方法
- Android基于高德地圖poi的仿微信獲取位置功能實例代碼
- Android 高德地圖之poi搜索功能的實現(xiàn)代碼
- Android高德地圖poi檢索仿微信發(fā)送位置實例代碼
- Android開發(fā)之高德地圖實現(xiàn)定位
- Android之高德地圖定位SDK集成及地圖功能實現(xiàn)
- 關(guān)于Android高德地圖的簡單開發(fā)實例代碼(DEMO)
- Android實現(xiàn)高德地圖顯示及定位
相關(guān)文章
Android編程實現(xiàn)的重力感應(yīng)示例代碼
這篇文章主要介紹了Android編程實現(xiàn)的重力感應(yīng)效果,以完整示例代碼形式分析了重力感應(yīng)的原理與實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10
Android Naive與WebView的互相調(diào)用詳解
這篇文章主要介紹了Android Naive與WebView的互相調(diào)用詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android自定義View實現(xiàn)箭頭沿圓轉(zhuǎn)動實例代碼
這篇文章主要介紹了Android自定義View實現(xiàn)箭頭沿圓轉(zhuǎn)動實例代碼,需要的朋友可以參考下2017-09-09
Android 中Fragment與Activity通訊的詳解
這篇文章主要介紹了Android 中Fragment與Activity通訊的詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握如何通信的,需要的朋友可以參考下2017-10-10

