地图-导航(百度/高德)

代码块:

public class MapNavigationDialog {
    private Activity activity;
    private AlertDialog dialog;

    private Button mBtnCancel;
    private Button mBtnEnsure;

    private LatLong latLng ;

    public MapNavigationDialog(Activity activity ) {
        this.activity = activity;
    }

    public void mSetLatLng( LatLong latLng ) {
        this.latLng = latLng;
    }

    public void show() {
        dialog = new AlertDialog.Builder(activity).create();
        dialog.show();
        Window window = dialog.getWindow();
        window.setBackgroundDrawable(new ColorDrawable(0));
        window.setBackgroundDrawable(null);
        window.setContentView(R.layout.dialog_yes_no);

        mBtnCancel = (Button) window.findViewById(R.id.btn_cancel);
        mBtnEnsure = (Button) window.findViewById(R.id.btn_ensure);

        mBtnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //百度线路规划
                //                origin    起点名称或经纬度,或者可同时提供名称和经纬度,此时经纬度优先级高,将作为导航依据,名称只负责展示    origin和destination
                CoordinateConverter converter  = new CoordinateConverter();
                converter.from(CoordinateConverter.CoordType.GPS);
                // sourceLatLng待转换坐标
                converter.coord(new LatLng(latLng.getLatitude(),latLng.getLongitude()));
                LatLng desLatLng = converter.convert();
                Intent i1 = new Intent();
                // 公交路线规划  direction?region=北京&origin=39.98871,116.43234&destination=40.055878,116.307854mode=transit&index=0&target=1 index=0&target=1
                i1.setData(Uri.parse("baidumap://map/direction?" +
                        "destination="+ desLatLng.latitude+"," + desLatLng.longitude +  //结束点
                        "&mode=driving&sy=0&index=0&target=1"));
                if (isInstallByread("com.baidu.BaiduMap")) {
                    dialog.dismiss();
                    activity.startActivity(i1);
                }  else {
                    Toast.makeText(activity, "没有安装百度地图!", Toast.LENGTH_SHORT).show();
                }
                dialog.dismiss();
            }
        });
        mBtnEnsure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 高德线路规划
                LatLng latLng = transformFromWGSToGCJ(new LatLng(MapNavigationDialog.this.latLng.getLatitude(), MapNavigationDialog.this.latLng.getLongitude()));
                try {
                    Intent intent = Intent.getIntent("amapuri://route/plan/?" +
                            "dlat=" + latLng.latitude +
                            "&dlon=" + latLng.longitude +
                            "&dname=终点&dev=0&t=0");
                    if (isInstallByread("com.autonavi.minimap")) {
                        dialog.dismiss();
                        activity.startActivity(intent);
                    } else {
                        Toast.makeText(activity, "没有安装高德地图!", Toast.LENGTH_SHORT).show();
                    }

                } catch (URISyntaxException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**
     * 判断是否安装目标应用
     *
     * @param packageName 目标应用安装后的包名
     * @return 是否已安装目标应用
     */
    private boolean isInstallByread(String packageName) {
        return new File("/data/data/" + packageName).exists();
    }

    public void dismiss() {
        dialog.dismiss();
    }

    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;
    }
}

xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@null"
    android:gravity="center"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp"
    >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="@drawable/bg_dialog_toolbar">
        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="25dp"
            android:layout_marginLeft="25dp"
            android:layout_centerVertical="true"
            android:gravity="center"
            android:text="请选择导航地图"
            android:textColor="#ffffff"
            android:textSize="15sp" />
    </RelativeLayout>

    <TextView
            android:id="@+id/tv_info"
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:layout_centerVertical="true"
            android:gravity="center"
            android:text="前往导航"
            android:background="#ffffff"
            android:textColor="#000000"
            android:textSize="18sp" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@null"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@null">

            <Button
                android:id="@+id/btn_cancel"
                android:layout_width="0dip"
                android:layout_height="45dp"
                android:layout_centerVertical="true"
                android:layout_weight="1"
                android:background="@drawable/bg_bt_left"
                android:gravity="center"
                android:text="百度导航"
                android:textColor="#ffffff"
                android:textSize="18sp" />

            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="@android:color/white" />

            <Button
                android:id="@+id/btn_ensure"
                android:layout_width="0dip"
                android:layout_height="45dp"
                android:layout_centerVertical="true"
                android:layout_weight="1"
                android:background="@drawable/bg_bt_right"
                android:gravity="center"
                android:text="高德导航"
                android:textColor="#ffffff"
                android:textSize="18sp" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

注意:
百度地图提供了直接步行导航和驾车导航,高德只提供了驾车导航;
上述实现的是线路导航:后续可手选是步行/驾车来进行导航.

效果:
高德导航界面
百度导航界面

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值