快捷搜索: 王者荣耀 脱发

高德地图百度地图导航坐标转换

项目中的定位商家服务搜索用的是高德地图,毕竟高德在地图这块做的比百度好(个人认为)。这里吐槽下百度的开发平台,上面开发文档及案例写的真不好,想找某个问题点很难找到和定位,希望百度改善下。

高德转百度(火星坐标gcj02ll–>百度坐标bd09ll)

private double[] gaoDeToBaidu(double gd_lon, double gd_lat) {
    double[] bd_lat_lon = new double[2];
    double PI = 3.14159265358979324 * 3000.0 / 180.0;
    double x = gd_lon, y = gd_lat;
    double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * PI);
    double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * PI);
    bd_lat_lon[0] = z * Math.cos(theta) + 0.0065;
    bd_lat_lon[1] = z * Math.sin(theta) + 0.006;
    return bd_lat_lon;
}

百度转高德(百度坐标bd09ll–>火星坐标gcj02ll)

private double[] bdToGaoDe(double bd_lat, double bd_lon) {
    double[] gd_lat_lon = new double[2];
    double PI = 3.14159265358979324 * 3000.0 / 180.0;
    double x = bd_lon - 0.0065, y = bd_lat - 0.006;
    double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * PI);
    double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * PI);
    gd_lat_lon[0] = z * Math.cos(theta);
    gd_lat_lon[1] = z * Math.sin(theta);
    return gd_lat_lon;
 }

调用高德导航: 1.AndroidAPI调用方式

/***
     * 
     * @author James
     * @Description 调用高德去导航
     * @param mLatLng
     * @param type
     */
    public static void startNavByAmap(LatLng mLatLng, int type, Context mContext) {

        try {
            // 构造导航参数
            NaviPara naviPara = new NaviPara();
            // 设置终点位置
            naviPara.setTargetPoint(mLatLng);
            // 设置导航策略,这里是避免拥堵
            naviPara.setNaviStyle(type);
            // 调起高德地图导航
            AMapUtils.openAMapNavi(naviPara, mContext);

        } catch (AMapException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

2,URI调用方式(不想下载SDK包使用URI调用也很方便)

Intent gddtIntent = new Intent("android.intent.action.VIEW",
                    Uri.parse("androidamap://navi?sourceApplication=“name”&poiname="
                            + poi + "&lat=" + lat + "&lon=" + lon + "&dev=" + 0
                            + "&style=" + 4));
            gddtIntent.addCategory("android.intent.category.DEFAULT");
            gddtIntent.setPackage("com.autonavi.minimap");
            gddtIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(gddtIntent);

参数 style导航策略

调用百度导航

1.AndroidAPI调用方式

double mLat1 = 39.915291;
double mLon1 = 116.403857;
// 百度大厦坐标
double mLat2 = 40.056858;
double mLon2 = 116.308194;
LatLng pt1 = new LatLng(mLat1, mLon1);
LatLng pt2 = new LatLng(mLat2, mLon2);

NaviParaOption para = new NaviParaOption()
                .startPoint(pt1).endPoint(pt2)
                .startName("天安门").endName("百度大厦");

        try {
// 调起百度地图导航
            BaiduMapNavigation.openBaiduMapNavi(para, this);
        } catch (BaiduMapAppNotSupportNaviException e) {
            e.printStackTrace();
        }

2,URI调用方式

intent = Intent
             .getIntent("intent://map/navi?location="
             + lonlat[1]
             + ","
             + lonlat[0]
             + "&type="
             + strategyBD
             +
             "&coordType=bd09ll&src=thirdapp.navi.公司名字#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
             mContext.startActivity(intent); // 启动调用

参数 strategyBD 导航策略

经验分享 程序员 微信小程序 职场和发展