高德地图定位,搜索,导航功能

这个demo其实是我为了熟悉高德地图写出来的,写的时候是我第一次接触高德,所以有写的不好的地方求不喷。

整个实现的过程大致如下:

    1:SDK集成

    2:界面绘制

    3:实现当前定位

    4:实现位置搜索

    5:根据起点与终点的经纬度进行导航

SDK下载

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

    <com.mancj.materialsearchbar.MaterialSearchBar
        android:id="@+id/searchBar"
        style="@style/MaterialSearchBarLight"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        app:mt_hint="输入目的地"
        app:mt_speechMode="true" />

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/searchBar"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp"
        android:background="#ffffff" />

</RelativeLayout>
 //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState),创建地图
        mMapView.onCreate(savedInstanceState);

        //初始化地图控制器对象
        if (aMap == null) {
            aMap = mMapView.getMap();
            UiSettings settings = aMap.getUiSettings();
            aMap.setLocationSource(this);//设置了定位的监听
            // 是否显示定位按钮
            settings.setMyLocationButtonEnabled(true);
            aMap.setMyLocationEnabled(true);//显示定位层并且可以触发定位,默认是flase
        }
private void location() {
        //初始化定位
        mLocationClient = new AMapLocationClient(getApplicationContext());
        //设置定位回调监听
        mLocationClient.setLocationListener(this);
        //初始化定位参数
        mLocationOption = new AMapLocationClientOption();
        //设置定位模式为Hight_Accuracy高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置是否强制刷新WIFI,默认为强制刷新
        mLocationOption.setWifiActiveScan(true);
        //设置定位间隔 默认采用连续定位模式,时间间隔为2000ms 可以自定义
        mLocationOption.setInterval(2000);
        //设置是否返回地址信息,默认返回
        mLocationOption.setNeedAddress(true);
        //设置是否允许模拟软件Mock位置结果,多为模拟GPS定位结果,默认为true,允许模拟位置
        mLocationOption.setMockEnable(true);
        //设置定位请求超时时间,默认为30秒
        mLocationOption.setHttpTimeOut(20000);
        //设置是否开启定位缓存机制 默认开启
        mLocationOption.setLocationCacheEnable(false);

        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //启动定位
        mLocationClient.startLocation();
    }

以上是部分关于定位的代码

我这边是将定位搜索导航都整合了一下

源码可以去git上下载,我这边就不多说了

代码传送门

喜欢请star,不喜欢也请star

猜你喜欢

转载自blog.csdn.net/sinat_39326775/article/details/80843158
今日推荐