天天看點

高德地圖路線規劃

1.高德地圖路線規劃

2.根據兩個點的經緯度,描繪出兩個點的步行路徑

3.根據百度的坐标點轉換成高德坐标點進行規劃路線

4.描繪出我目前的位置

5.描繪出目的地的位置

6.初始化地圖,我的位置,進行适當的縮放地圖,滿足業務需要.

7.直接上代碼

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdate;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.CameraPosition;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.amap.api.maps2d.model.Polyline;
import com.amap.api.maps2d.model.PolylineOptions;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.route.BusRouteResult;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.RouteSearch.FromAndTo;
import com.amap.api.services.route.RouteSearch.OnRouteSearchListener;
import com.amap.api.services.route.RouteSearch.WalkRouteQuery;
import com.amap.api.services.route.WalkPath;
import com.amap.api.services.route.WalkRouteResult;
import com.amap.api.services.route.WalkStep;

import hyf.wonengkeji.com.util.HyfToast;

public class MainGaoDeMap extends Activity implements OnRouteSearchListener {

    private Double position_tv_x;
    private Double position_tv_y;
    private String name;
    private Double x;
    private Double y;

    //地圖控件
    private AMap aMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_gao_de_map);

        //初始化接受資料
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        position_tv_x = (Double) bundle.get("position_tv_x");
        position_tv_y = (Double) bundle.get("position_tv_y");
        name = bundle.getString("name");
        x = (Double) bundle.get("x");
        y = (Double) bundle.get("y");


        //bd_decrypt01(position_tv_x, position_tv_y);
        //bd_decrypt02(x, y);


        //加載地圖
        MapView mapView = (MapView) findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);// 此方法必須重寫
        aMap = mapView.getMap();
        aMap.clear();


        //初始化我的位置
        setMyPosition(position_tv_x, position_tv_y);

        //設定目的地的位置
        setPositionPoint(name, x, y);

        //我的标注點
        setMinePositionPoint("我的位置", position_tv_x, position_tv_y, "我的位置");

        //繪制線路
        setLine(position_tv_x, position_tv_y, x, y);

    }


    /**
     * 初始化我的位置地圖
     *
     * @param x
     * @param y
     */
    private void setMyPosition(Double x, Double y) {
        //初始化點
        CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(new CameraPosition(
                new LatLng(x, y),//新的中心點坐标
                , //新的縮放級别
                , //俯仰角0°~45°(垂直與地圖時為0)
                  偏航角 0~360° (正北方為0)
        ));
        aMap.moveCamera(cameraUpdate);

    }

    //加載目的地的位置
    private void setPositionPoint(String name, Double x, Double y) {
        LatLng latLng = new LatLng(x, y);

        MarkerOptions markerOption = new MarkerOptions();
        markerOption.position(latLng);
        markerOption.title(name);//.snippet(name)
        // 将Marker設定為貼地顯示,可以雙指下拉看效果
        //markerOption.setFlat(true);
        Marker marker = aMap.addMarker(markerOption);

        View view = LayoutInflater.from(this).inflate(
                R.layout.business_map, null);
        TextView tv01 = (TextView) view.findViewById(R.id.tv01);
        tv01.setText(name);
        marker.setIcon(BitmapDescriptorFactory.fromView(view));
        marker.showInfoWindow();

    }


    /********************************
     * 我的位置
     ****************************************/
    private void setMinePositionPoint(String name, Double x, Double y, String str) {

        LatLng latLng = new LatLng(x, y);

        MarkerOptions markerOption = new MarkerOptions();
        markerOption.position(latLng);
        markerOption.title(name);//.snippet(name)
        // 将Marker設定為貼地顯示,可以雙指下拉看效果
        //markerOption.setFlat(true);
        final Marker marker = aMap.addMarker(markerOption);

        View view = LayoutInflater.from(this).inflate(
                R.layout.mappoaition_marker, null);
        marker.setIcon(BitmapDescriptorFactory.fromView(view));
        marker.showInfoWindow();
    }
    /********************************我的位置****************************************/


    /********************************繪制線路************************************/
    /**
     * 繪制線路
     *
     * @param x1
     * @param y1
     * @param x2
     * @param y2
     */
    private void setLine(Double x1, Double y1,
                         Double x2, Double y2) {
        LatLonPoint la01 = new LatLonPoint(x1, y1);
        LatLonPoint la02 = new LatLonPoint(x2, y2);
        workspace(new FromAndTo(la01, la02), );
    }

    /**
     * 實作方法
     *
     * @param fromAndTo
     * @param walkMode
     */
    public void workspace(FromAndTo fromAndTo, int walkMode) {
        final RouteSearch routeSearch = new RouteSearch(this);
        routeSearch.setRouteSearchListener(this);
        WalkRouteQuery query = new WalkRouteQuery(fromAndTo, walkMode);
        routeSearch.calculateWalkRouteAsyn(query);//開始算路
    }

    /***********************
     * 回調的監聽方法
     ***************************/
    @Override
    public void onBusRouteSearched(BusRouteResult result, int rCode) {
    }

    @Override
    public void onDriveRouteSearched(DriveRouteResult result, int rCode) {
    }
    /*@Override
    public void onRideRouteSearched(RideRouteResult result, int rCode) {
    }*/

    //步行路線
    @Override
    public void onWalkRouteSearched(WalkRouteResult result, int rCode) {
        if (rCode == ) {//成功
            List<WalkPath> list_path = result.getPaths();
            for (int i = ; i < list_path.size(); i++) {
                List<WalkStep> list_step = list_path.get(i).getSteps();
                for (int j = ; j < list_step.size(); j++) {
                    List<LatLonPoint> listlatlone = list_step.get(j).getPolyline();
                    //畫線
                    List<LatLng> latLngs = new ArrayList<LatLng>();
                    for (int k = ; k < listlatlone.size(); k++) {
                        latLngs.add(new LatLng(listlatlone.get(k).getLatitude(), listlatlone.get(k).getLongitude()));

                    }
                    Polyline polyline = aMap.addPolyline(new PolylineOptions().
                            addAll(latLngs)
                            .width()//設定線寬度
                            //.setUseTexture(true)//設定紋理貼圖
                            .setDottedLine(true)//設定虛線
                            //.color(Color.argb(255, 77, 220, 38)));//設定線的顔色
                            .color());//設定線的顔色

                }
            }
        } else {
            //Toast.makeText(this, "定位失敗", 0).show();
            HyfToast.showToast(this, "定位失敗");
        }
    }
    /***********************回調的監聽方法***************************/
    /********************************
     * 繪制線路
     ************************************/


    public void doclick(View v) {
        switch (v.getId()) {
            case R.id.all_back_rl:
                finish();
                overridePendingTransition(R.anim.ap2_300, R.anim.ap1_300);// 淡出淡入動畫效果
                break;
        }
    }

    /*****
     * 觸摸關閉  回調
     *****/
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        finish();
        overridePendingTransition(R.anim.ap2_300, R.anim.ap1_300);// 淡出淡入動畫效果
        return true;
    }

    /*****
     * 監聽傳回鍵  回調
     *****/
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        finish();
        overridePendingTransition(R.anim.ap2_300, R.anim.ap1_300);// 淡出淡入動畫效果
        return super.onKeyDown(keyCode, event);
    }

    //百度地圖轉高德地圖
    double x_pi =  *  / ;

    public void bd_decrypt02(double bd_lat, double bd_lon) {
        double x = bd_lon - , y = bd_lat - ;
        double z = Math.sqrt(x * x + y * y) -  * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) -  * Math.cos(x * x_pi);
        x = z * Math.cos(theta);
        y = z * Math.sin(theta);
    }

    public void bd_decrypt01(double bd_lat, double bd_lon) {
        double x = bd_lon - , y = bd_lat - ;
        double z = Math.sqrt(x * x + y * y) -  * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) -  * Math.cos(x * x_pi);
        position_tv_y = z * Math.cos(theta);
        position_tv_x = z * Math.sin(theta);
    }

}
           

一.這是一個完整的類,隻需要傳遞5個參數即可實作需求.

自己的經緯度

目的地的經緯度+名稱.(項目需要)

二.順便也貼出兩個布局檔案

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main_gao_de_map"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.amap.api.maps2d.MapView
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"></com.amap.api.maps2d.MapView>

        <RelativeLayout
            android:id="@+id/all_back_rl"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:onClick="doclick">

            <TextView
                android:layout_width="28dp"
                android:layout_height="28dp"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:background="@mipmap/back_80" />

        </RelativeLayout>


        <!--<RelativeLayout
            android:id="@+id/all_back_rl"
            android:layout_width="36dp"
            android:layout_height="36dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/mian_ffffff_5_5_5_5"
            android:onClick="doclick">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:text="X"
                android:textColor="@color/tvffffff"
                android:textSize="20dp" />
        </RelativeLayout>-->
    </RelativeLayout>


</RelativeLayout>
           
這裡寫代碼片<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="200dp"
        android:layout_height="50dp">

        <ImageView
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:src="@mipmap/mapposition01" />

        <TextView
            android:id="@+id/tv01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="110dp"
            android:background="#517efd"
            android:ellipsize="end"
            android:maxLines="2"
            android:padding="5dp"
            android:text="88888888"
            android:textColor="@color/tvffffff"
            android:textSize="@dimen/tv16" />
    </RelativeLayout>

</LinearLayout>
           
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="240dp"
        android:layout_height="36dp">

        <RelativeLayout
            android:id="@+id/hz_rl"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:background="@mipmap/dingwei_my">

        </RelativeLayout>

        <TextView
            android:id="@+id/tv_position"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="3dp"
            android:layout_toRightOf="@+id/hz_rl"
            android:ellipsize="end"
            android:maxLines="2"
            android:text=""
            android:textColor="#000000"
            android:textSize="@dimen/tv12" />

    </RelativeLayout>

</RelativeLayout>
           

(1).布局很喽,可以忽略.

(2).基本上高德的2dmap包導一個就可以,其他的細節請各位稍加完善即可.

//附上我們上線的應用,大家下載下傳玩一下,有好處的喲!

http://info.appstore.vivo.com.cn/detail/1841175?source=1

繼續閱讀