Android 根据地址调手机的高德,百度,腾讯地图导航

首先切记利用到地图,万物归结于经纬度

效果图:


效果图.png

1.基于高德的经纬度(也是基于高德坐标系)

配置:导入AMap_Location_Vxxx.jar和AMap_Search_Vxxx.jar。这里编辑器文件,应该看得懂的
高德一些其他配置就不说了,按高德定位SDK配置就行

先根据地址获取高德地图的经纬度

private fun getLatlon(context:Context,cityName: String,selectItem:String) {
    val geocodeSearch = GeocodeSearch(context)
    geocodeSearch.setOnGeocodeSearchListener(object : GeocodeSearch.OnGeocodeSearchListener {

        override fun onRegeocodeSearched(p0: RegeocodeResult?, p1: Int) {

        }

        override fun onGeocodeSearched(geocodeResult: GeocodeResult?, i: Int) {
            if (i == 1000) {
                if (geocodeResult?.geocodeAddressList != null && geocodeResult.geocodeAddressList.size > 0) {
                    val geocodeAddress: GeocodeAddress = geocodeResult.geocodeAddressList.get(0)
                    val latitude: Double = geocodeAddress.latLonPoint.getLatitude() //纬度
                    val longititude: Double = geocodeAddress.latLonPoint.getLongitude() //经度
                    val adcode: String = geocodeAddress.getAdcode() //区域编码
                    Log.e("lgq地理编码", geocodeAddress.getAdcode().toString() + "")
                    Log.e("lgq纬度latitude", latitude.toString() + "")
                    Log.e("lgq经度longititude", longititude.toString() + "")
                    Log.i("lgq", "dddwww====$longititude")
                    when(selectItem){
                        "高德地图" -> {
                            MapUtil.openGaoDeNavi(context,0.0,0.0,null,latitude,longititude,cityName)
                            //MapUtil.openGaoDeNavi(mActivity,0.0,0.0,null,)
                        }
                        "百度地图" -> {
                            MapUtil.openBaiDuNavi(context,0.0,0.0,null,latitude,longititude,cityName)
                        }
                        "腾讯地图" -> {
                            MapUtil.openTencentMap(context,0.0,0.0,null,latitude,longititude,cityName)
                        }
                    }

                    //getCityAddress(longititude, latitude)
                } else {
                    ToastUtils.showCustomToast("地名出错")

                }
            }
        }

    })
    val geocodeQuery = GeocodeQuery(cityName.trim { it <= ' ' }, "29")
    geocodeSearch.getFromLocationNameAsyn(geocodeQuery)
}

2.完整关键的的MapUtil

public class MapUtil {

public static final String PN_GAODE_MAP = "com.autonavi.minimap";// 高德地图包名
public static final String PN_BAIDU_MAP = "com.baidu.BaiduMap"; // 百度地图包名
public static final String PN_TENCENT_MAP = "com.tencent.map"; // 腾讯地图包名

/**
 * 检查地图应用是否安装
 * @return
 */
public static boolean isGdMapInstalled(){
    return isInstallPackage(PN_GAODE_MAP);
}

public static boolean isBaiduMapInstalled(){
    return isInstallPackage(PN_BAIDU_MAP);
}

public static boolean isTencentMapInstalled(){
    return isInstallPackage(PN_TENCENT_MAP);
}

private static boolean isInstallPackage(String packageName) {
    return new File("/data/data/" + packageName).exists();
}

/**
 * 百度转高德
 * @param bd_lat
 * @param bd_lon
 * @return
 */
public static 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;
}

/**
 * 高德、腾讯转百度
 * @param gd_lon
 * @param gd_lat
 * @return
 */
private static 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;
}


/**
 * 打开高德地图导航功能
 * @param context
 * @param slat 起点纬度
 * @param slon 起点经度
 * @param sname 起点名称 可不填(0,0,null)
 * @param dlat 终点纬度
 * @param dlon 终点经度
 * @param dname 终点名称 必填
 */
public static void openGaoDeNavi(Context context,double slat, double slon, String sname, double dlat, double dlon, String dname){
    String uriString = null;
    StringBuilder builder = new StringBuilder("amapuri://route/plan?sourceApplication=maxuslife");
    if (slat != 0) {
        builder.append("&sname=").append(sname)
                .append("&slat=").append(slat)
                .append("&slon=").append(slon);
    }
    builder.append("&dlat=").append(dlat)
            .append("&dlon=").append(dlon)
            .append("&dname=").append(dname)
            .append("&dev=0")
            .append("&t=0");
    uriString = builder.toString();
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setPackage(PN_GAODE_MAP);
    intent.setData(Uri.parse(uriString));
    context.startActivity(intent);
}

/**
 * 打开腾讯地图
 * params 参考http://lbs.qq.com/uri_v1/guide-route.html
 *
 * @param context
 * @param slat 起点纬度
 * @param slon 起点经度
 * @param sname 起点名称 可不填(0,0,null)
 * @param dlat 终点纬度
 * @param dlon 终点经度
 * @param dname 终点名称 必填
 * 驾车:type=drive,policy有以下取值
0:较快捷
1:无高速
2:距离
policy的取值缺省为0
 * &from=" + dqAddress + "&fromcoord=" + dqLatitude + "," + dqLongitude + "
 */
public static void openTencentMap(Context context, double slat, double slon, String sname, double dlat, double dlon, String dname) {
    String uriString = null;
    StringBuilder builder = new StringBuilder("qqmap://map/routeplan?type=drive&policy=0&referer=zhongshuo");
    if (slat != 0) {
        builder.append("&from=").append(sname)
                .append("&fromcoord=").append(slat)
                .append(",")
                .append(slon);
    }
    builder.append("&to=").append(dname)
            .append("&tocoord=").append(dlat)
            .append(",")
            .append(dlon);
    uriString = builder.toString();
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setPackage(PN_TENCENT_MAP);
    intent.setData(Uri.parse(uriString));
    context.startActivity(intent);
}

/**
 * 打开百度地图导航功能(默认坐标点是高德地图,需要转换)
 * @param context
 * @param slat 起点纬度
 * @param slon 起点经度
 * @param sname 起点名称 可不填(0,0,null)
 * @param dlat 终点纬度
 * @param dlon 终点经度
 * @param dname 终点名称 必填
 */
public static void openBaiDuNavi(Context context,double slat, double slon, String sname, double dlat, double dlon, String dname){
    String uriString = null;


    double destination[] = gaoDeToBaidu(dlat, dlon);
    dlat = destination[0];
    dlon = destination[1];

    StringBuilder builder = new StringBuilder("baidumap://map/direction?mode=driving&");
    if (slat != 0){
        //起点坐标转换



        double[] origin = gaoDeToBaidu(slat, slon);
        slat = origin[0];
        slon = origin[1];

        builder.append("origin=latlng:")
                .append(slat)
                .append(",")
                .append(slon)
                .append("|name:")
                .append(sname);
    }
    builder.append("&destination=latlng:")
            .append(dlat)
            .append(",")
            .append(dlon)
            .append("|name:")
            .append(dname);
    uriString = builder.toString();
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setPackage(PN_BAIDU_MAP);
    intent.setData(Uri.parse(uriString));
    context.startActivity(intent);
}
}

顺便提供下前面判断和弹框等的代码

fun selectMapToLocation(context:Context,address: String?) {
    if (address.isNullOrBlank()) return
    var localMaps = mutableListOf<String>()
    if (MapUtil.isGdMapInstalled()) {
        localMaps.add("高德地图")
    }
    if (MapUtil.isBaiduMapInstalled()) {
        localMaps.add("百度地图")
    }
    if (MapUtil.isTencentMapInstalled()) {
        localMaps.add("腾讯地图")
    }
    if (localMaps.isNullOrEmpty()){
        ToastUtils.showCustomToast(context.getString(R.string.no_map))
        return
    }
    DialogHelper.showSelectDialog(context, localMaps, object : DialogHelper.SelectItemListener {
        override fun clickPosition(position: Int) {
            var selectItem = ""
            for (i in localMaps.indices) {
                if (localMaps.get(i) == localMaps.get(position)) {
                    selectItem = localMaps.get(i);
                    break
                }
            }
            getLatlon(context,address, selectItem)
        }

    })
}

fun showSelectDialog(context: Context,items:MutableList<String>,selectItemListener: SelectItemListener?){
    var dialog = getCancelDialog(context)
    dialog.show()
    val window = dialog.window
    window.setGravity(Gravity.BOTTOM)
    window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
    window.setContentView(R.layout.dialog_items)

    val rv = window.findViewById<RecyclerView>(R.id.rv_items)
    rv.layoutManager = LinearLayoutManager(context)

    val adapter = DialogSelectItemAdapter(data = items)
    rv.adapter = adapter

    adapter.setOnItemClickListener { adapter, view, position ->
        selectItemListener?.clickPosition(position)
        dialog.dismiss()
    }

    val tv_cancel = window.findViewById<TextView>(R.id.tv_cancel)
    tv_cancel.setOnClickListener {
        dialog.dismiss()
    }
}

interface SelectItemListener{
    fun clickPosition(position:Int)
}

总结:关键是MapUtil工具类,有问题请私信我或者评论,我每天都看简书的

推荐阅读更多精彩内容