高德地圖-搜索-路徑規劃

一、打開高德開放平臺
二、建立應用android

SHA1: F5:31:C2:B7:9F:A4:05:DF:5F:B2:F1:70:9B:03:B9:BB:50:CF:58:CB
三、添加新key:dd9dde5da25a5811031cb1beb5acb7a5
    sha1值獲取:找到系統默認的debug.keystore(C:\Users\Administrator\.android\debug.keystore)
                在cmd窗口或android studio 中的Terminal中執行命令:keytool -v -list -keystore keystore地址
四、添加jar和so庫(不要添加v4包)
    so庫添加方式:
        第一種:使用默認配置,不須要修改build.gradle。在 main 目錄下建立文件夾 jniLibs (若是有就不須要建立了),將下載文件的 armeabi 文件夾複製到這個目錄下,若是已經有這個目錄,將下載的 so 庫複製到這個目錄便可。
        第二種:使用自定義配置,將下載文件的 armeabi 文件夾複製到 libs 目錄,若是有這個目錄,請將下載的 so 庫複製到這個目錄,git

而後打開build.gradle,找到 android標籤複製:api

sourceSets{
    main{
        jniLibs.srcDirs = ['libs']
    }
}


五、配置
    一、配置AndroidManifest.xml
        一、聲明Service組件(不須要)
            <service android:name="com.amap.api.location.APSService"></service>
        二、聲明權限
            緩存

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.diyu">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!--設置高德Key必定要新申請-->
        <meta-data
            android:name="com.amap.api.v2.apikey"
            android:value="bad221b1acc389b8213114127d325855">
        </meta-data>
    </application>


    <!--用於進行網絡定位-->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
    <!--用於訪問GPS定位-->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
    <!--用於獲取運營商信息,用於支持提供運營商信息相關的接口-->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    <!--用於訪問wifi網絡信息,wifi信息會用於進行網絡定位-->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
    <!--用於獲取wifi的獲取權限,wifi信息會用來進行網絡定位-->
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
    <!--用於訪問網絡,網絡定位須要上網-->
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <!--用於讀取手機當前的狀態-->
    <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
    <!--用於寫入緩存數據到擴展存儲卡-->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    <!--用於申請調用A-GPS模塊-->
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission>
    <!--用於申請獲取藍牙信息進行室內定位-->
    <uses-permission android:name="android.permission.BLUETOOTH"></uses-permission>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"></uses-permission>

</manifest>

----------------------------------------------下面是代碼實現----------------------------------------------網絡

public class MainActivity extends AppCompatActivity implements RouteSearch.OnRouteSearchListener, View.OnClickListener {


    private MapView map;
    private AMap aMap;
    private EditText et1;
    private EditText et2;
    private Button but;
    private PoiSearch poiSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        map.onCreate(savedInstanceState);
        initMap();
    }

    private void initMap() {
        aMap = map.getMap();

        MyLocationStyle myLocationStyle;
        myLocationStyle = new MyLocationStyle();//初始化定位藍點樣式類myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//連續定位、且將視角移動到地圖中心點,定位點依照設備方向旋轉,而且會跟隨設備移動。(1秒1次定位)若是不設置myLocationType,默認也會執行此種模式。
        myLocationStyle.interval(20000); //設置連續定位模式下的定位間隔,只在連續定位模式下生效,單次定位模式下不會生效。單位爲毫秒。
        aMap.setMyLocationStyle(myLocationStyle);//設置定位藍點的Style
        aMap.getUiSettings().setMyLocationButtonEnabled(true);//設置默認定位按鈕是否顯示,非必需設置。
        aMap.setMyLocationEnabled(true);// 設置爲true表示啓動顯示定位藍點,false表示隱藏定位藍點並不進行定位,默認是false。
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);
        myLocationStyle.showMyLocation(true);


        aMap.setOnMarkerClickListener(new AMap.OnMarkerClickListener() {

            private RouteSearch routeSearch;

            @Override
            public boolean onMarkerClick(Marker marker) {
                routeSearch = new RouteSearch(MainActivity.this);
                routeSearch.setRouteSearchListener(MainActivity.this);
                //獲取起點經緯度
                Location myLocation = aMap.getMyLocation();
                double latitude = myLocation.getLatitude();
                double longitude = myLocation.getLongitude();
                LatLonPoint point = new LatLonPoint(latitude, longitude);
                //獲取終點經緯度
                LatLng position = marker.getPosition();
                double latitude1 = position.latitude;
                double longitude1 = position.longitude;
                LatLonPoint point1 = new LatLonPoint(latitude1, longitude1);
                RouteSearch.FromAndTo fat = new RouteSearch.FromAndTo(point,point1);
                RouteSearch.DriveRouteQuery query = new RouteSearch.DriveRouteQuery(fat,RouteSearch.DRIVING_MULTI_STRATEGY_FASTEST_SAVE_MONEY_SHORTEST , null, null, "");
                //發送請求
                routeSearch.calculateDriveRouteAsyn(query);

                return false;
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity執行onDestroy時執行mMapView.onDestroy(),銷燬地圖
        map.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity執行onResume時執行mMapView.onResume (),從新繪製加載地圖
        map.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity執行onPause時執行mMapView.onPause (),暫停地圖的繪製
        map.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity執行onSaveInstanceState時執行mMapView.onSaveInstanceState (outState),保存地圖當前的狀態
        map.onSaveInstanceState(outState);
    }

    private void initView() {
        map = (MapView) findViewById(R.id.map);
        et1 = (EditText) findViewById(R.id.et1);
        et1.setOnClickListener(this);
        et2 = (EditText) findViewById(R.id.et2);
        et2.setOnClickListener(this);
        but = (Button) findViewById(R.id.but);
        but.setOnClickListener(this);
    }

    @Override
    public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {
        //公交查詢結果
    }

    @Override
    public void onDriveRouteSearched(DriveRouteResult result, int errorCode) {
        //自駕查詢結果
        aMap.clear();// 清理地圖上的全部覆蓋物
        if (errorCode == AMapException.CODE_AMAP_SUCCESS) {
            if (result != null && result.getPaths() != null) {
                if (result.getPaths().size() > 0) {

                    final DrivePath drivePath = result.getPaths()
                            .get(0);
                    DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay(
                            MainActivity.this, aMap, drivePath,
                            result.getStartPos(),
                            result.getTargetPos(), null);
                    drivingRouteOverlay.setNodeIconVisibility(false);//設置節點marker是否顯示
                    drivingRouteOverlay.setIsColorfulline(true);//是否用顏色展現交通擁堵狀況,默認true
                    drivingRouteOverlay.removeFromMap();
                    drivingRouteOverlay.addToMap();
                    drivingRouteOverlay.zoomToSpan();
                } else if (result != null && result.getPaths() == null) {
                    ToastUtil.show(MainActivity.this, R.string.no_result);
                }

            } else {
                ToastUtil.show(MainActivity.this, R.string.no_result);
            }
        } else {
            ToastUtil.showerror(this.getApplicationContext(), errorCode);
        }
    }

    @Override
    public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) {
        //步行查詢結果
    }

    @Override
    public void onRideRouteSearched(RideRouteResult rideRouteResult, int i) {
        //騎行查詢結果
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.but:
                submit();
                break;
        }
    }

    private void submit() {
        // validate
        String et1String = et1.getText().toString().trim();
        if (TextUtils.isEmpty(et1String)) {
            Toast.makeText(this, "et1String不能爲空", Toast.LENGTH_SHORT).show();
            return;
        }

        String et2String = et2.getText().toString().trim();
        if (TextUtils.isEmpty(et2String)) {
            Toast.makeText(this, "et2String不能爲空", Toast.LENGTH_SHORT).show();
            return;
        }

        // TODO validate success, do something
        PoiSearch.Query query = new PoiSearch.Query(et2String, "", et1String);
        query.setPageNum(1);
        query.setPageSize(10);

        poiSearch = new PoiSearch(this, query);
        poiSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
            @Override
            public void onPoiSearched(PoiResult poiResult, int i) {

                aMap.clear();

                for (PoiItem item : poiResult.getPois()) {
                    Log.e("TAG", item.getTitle() + "\n" + item.getAdName() + "\n" + item.getCityName() + "\n" + item.getLatLonPoint().toString());
                    LatLng latLng = new LatLng(item.getLatLonPoint().getLatitude(), item.getLatLonPoint().getLongitude());
                    final Marker marker = aMap.addMarker(new MarkerOptions().position(latLng).title(item.getTitle()).snippet(item.getSnippet()));
                }
            }

            @Override
            public void onPoiItemSearched(PoiItem poiItem, int i) {

            }
        });
        poiSearch.searchPOIAsyn();
    }

}
相關文章
相關標籤/搜索