Android中GPS坐标转换为高德地图坐标详解

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Android中GPS坐标转换为⾼德地图坐标详解
⼀、坐标分类
地图坐标⼤致分为⼏种:
1、GPS、WGS84,也就是原始坐标体系,这是国际公认的世界标准坐标体系;
2、GCJ-02,⼜称为“⽕星坐标”,国家测绘局在02年发布的坐标体系,在国内,⾄少得使⽤此坐标体系,⽐如:google、⾼德、腾讯地图等;
3、其他特殊坐标体系,⼀般都是由⽕星坐标通过偏移算法计算得出的,⽐如百度使⽤的是BD-09坐标,搜狗使⽤的是⾃⼰的搜狗坐标。

⼆、坐标转换
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、⾃⼰转换
public AMapLocation fromGpsToAmap(Location location) {
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
latLng = CoordinateUtil.transformFromWGSToGCJ(latLng);
AMapLocation aMapLocation = new AMapLocation(location);
aMapLocation.setLatitude(titude);
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(titude, wgLoc.longitude)) {
return new LatLng(titude, wgLoc.longitude);
}
double dLat = transformLat(wgLoc.longitude - 105.0,
titude - 35.0);
double dLon = transformLon(wgLoc.longitude - 105.0,
titude - 35.0);
double radLat = titude / 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(titude + 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;
}
}
总结
以上就是这篇⽂章的全部内容了,希望本⽂的内容对⼤家的学习或者⼯作能带来⼀定的帮助,如果有疑问⼤家可以留⾔交流。

相关文档
最新文档