Android简单实现gaode地图定位以及导航功能
1.环境的配置
1.1 在高德开放平台上获取key值。
1.2 导入项目需要的so文件以及jar文件。
目前使用的还是5.7版本的jar,需要的到文章底的github中下载
2 权限列表
<!-- 用于进行网络定位 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- 用于访问GPS定位 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- 获取运营商信息,用于支持提供运营商信息相关的接口 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位 -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位 -->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <!-- 用于访问网络,网络定位需要上网 -->
<uses-permission android:name="android.permission.INTERNET" /> <!-- 用于读取手机当前的状态 -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> <!-- 写入扩展存储,向扩展卡写入数据,用于写入缓存定位数据 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
以及在application中注册一下service
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="66a0691b3a07b08e699b57b2e728f6db" />
<service android:name="com.amap.api.location.APSService" />
注意,需要动态申请的权限记得自己申请,懒得写,demo也是直接手机手动给的权限。
3.核心代码
在导航开启前,需要获取起始点目的点,目的点随便选了个测试的点,起始点为定位的本地点。
实现定位功能,需要使activity继承并实现AMapLocationListener接口,首先在Oncreate()中初始化定位服务。
mLocationClient = new AMapLocationClient(getApplicationContext());
//设置定位回调监听
mLocationClient.setLocationListener(this);
//初始化AMapLocationClientOption对象
mLocationOption = new AMapLocationClientOption();
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
if (null != mLocationClient) {
mLocationClient.setLocationOption(mLocationOption);
mLocationClient.startLocation();
}
如果定位成功会回调onLocationChanged这个方法,在里面把获取到的定位信息存到本地
public void onLocationChanged(AMapLocation aMapLocation) {
if (aMapLocation != null) {
if (aMapLocation.getErrorCode() == 0) {
start_lat = aMapLocation.getLatitude();
start_lng = aMapLocation.getLongitude();
} else {
String errText = "定位失败," + aMapLocation.getErrorCode() + ": " + aMapLocation.getErrorInfo();
Log.e("AmapErr", errText);
}
}
}
再将点信息传到导航页面
Bundle bundle = new Bundle();
bundle.putDouble("start_lng", start_lng);
bundle.putDouble("start_lat", start_lat);
bundle.putDouble("end_lng", 120.213314);
bundle.putDouble("end_lat", 30.290953);
Intent intent = new Intent();
intent.setClass(MainActivity.this, NaviActivity.class);
intent.putExtras(bundle);
startActivity(new Intent(intent));
导航页面需要继承并实现AMapNaviListener, AMapNaviViewListener,页面xml如下
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.amap.api.navi.AMapNaviView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.constraint.ConstraintLayout>
在onInitNaviSuccess()方法中实现导航主要代码如下
AMapNavi amapNavi = null;
public void onInitNaviSuccess() {
System.out.println("TestActivity:" + "onInitNaviSuccess" + "----");
int strategy = 0;
try {
strategy = amapNavi.strategyConvert(true, false, false, false, false);
} catch (Exception e) {
e.printStackTrace();
}
List<NaviLatLng> start = new ArrayList<>();
NaviLatLng naviLatLng1 = new NaviLatLng(startLat, startLng);
start.add(naviLatLng1);
List<NaviLatLng> end = new ArrayList<>();
NaviLatLng naviLatLng2 = new NaviLatLng(endLat, endLng);
end.add(naviLatLng2);
amapNavi.calculateDriveRoute(start, end, null, strategy);
amapNavi.setUseInnerVoice(true);
}
在onCalculateRouteSuccess中模拟一下导航。
amapNavi.startNavi(NaviType.EMULATOR);
给一张效果图
github上的demo tps://github.com/wys919591169/GaodeNaviDemo,谢谢观看呀