1.添加key
打开https://lbs.qq.com/dev/console/application/mine 添加应用和key,勾选导【导航SDK(Android/iOS)】,否则会提示没有权限
2.配置AndroidManifest.xml文件
在application节点添加:
<meta-data
android:name="TencentMapSDK"
android:value="申请的api-key"/>
3.配置proguard-rules.pro文件,添加以下配置
-keep class com.tencent.map.**{ *; }
4.配置项目build.gradle
allprojects {
repositories {
google()
// maven { url 'https://dl.google.com/dl/android/maven2/' }
jcenter()
maven {url 'https://developer.huawei.com/repo/'}
//以下为腾讯地图新增
maven {
url "https://oss.sonatype.org/content/groups/public"
}
maven {
url "https://oss.sonatype.org/content/repositories/snapshots/"
}
maven {
url "https://oss.sonatype.org/content/repositories/staging/"
}
maven {
url "https://mirrors.tencent.com/repository/maven/tencent_public_snapshots"
}
}
}
5.配置app build.gradle
//腾讯地图 begin
// 地图库
implementation 'com.tencent.map:tencent-map-vector-sdk:4.5.10'
// 导航库
implementation 'com.tencent.map:tencent-map-nav-sdk:5.4.2.3'
// 导航依赖库
implementation 'com.tencent.map:tencent-map-nav-surport:1.1.0.1'
//腾讯地图 end
6.初始化
TencentMapInitializer.setAgreePrivacy(true);
TencentNavi.setUserAgreePrivacy(true);
var config = TencentNavi.Config();
// 记录设备标识,反馈导航问题时请提供该设备标识以及发生问题的时间
var deviceID = DeviceUtils.getImei(this)
config.deviceId=deviceID
// 或者设置开发者自己的的设备号config.setDeviceId(xxxxxxxx);
TencentNavi.init(this, config);
7.布局页
<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">
<com.tencent.map.navi.ride.RideNaviView
android:id="@+id/ride_navi_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
8.kotlin示例
package 包名
import android.Manifest
import android.os.Build
import android.os.Bundle
import android.os.Looper
import android.provider.Settings
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import 包名.R
import com.tencent.map.geolocation.TencentLocation
import com.tencent.map.geolocation.TencentLocationListener
import com.tencent.map.geolocation.TencentLocationManager
import com.tencent.map.navi.TencentRouteSearchCallback
import com.tencent.map.navi.data.CalcRouteResult
import com.tencent.map.navi.data.NaviPoi
import com.tencent.map.navi.data.RouteData
import com.tencent.map.navi.ride.RideNaviView
import com.tencent.map.navi.ride.RideRouteSearchOptions
import com.tencent.map.navi.ride.TencentRideNaviManager
import com.tencent.map.navi.ui.car.CarNaviInfoPanel
import pub.devrel.easypermissions.AfterPermissionGranted
import pub.devrel.easypermissions.EasyPermissions
class NaviRideActivity : AppCompatActivity() {
// 算路的起点
var mfrom: NaviPoi? = null
//终点
var mDest: NaviPoi? = null
private var mRideManager: TencentRideNaviManager? = null
private var mRideNaviView: RideNaviView? = null
private var mLocationManager: TencentLocationManager? = null
private val TAG = "NaviRideActivity"
private val onNaviInfoListener: CarNaviInfoPanel.OnNaviInfoListener =
CarNaviInfoPanel.OnNaviInfoListener { stopNavi() }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_navi_ride);
var latitude = intent.getDoubleExtra("latitude", 0.0)
var longitude = intent.getDoubleExtra("longitude", 0.0)
mDest = NaviPoi(latitude, longitude)
// 初始化导航控制类
mRideManager = TencentRideNaviManager(application);
// 导航视图 com.tencent.map.navi.ride.RideNaviView
mRideNaviView = findViewById<RideNaviView>(R.id.ride_navi_view);
// 将视图与控制器关联
// mRideManager?.addTencentNaviListener(mRideNaviView);
// 开启语音播报模块
// mRideManager?.setInternalTtsEnabled(true);
mLocationManager = TencentLocationManager.getInstance(this.application, null);
if (Build.VERSION.SDK_INT >= 23) {
requirePermission();
}
if (!judgeLocationServerState()) {
//没有打开位置服务开关,这里设计交互逻辑引导用户打开位置服务开关
} else {
this.mLocationManager?.requestSingleLocationFresh(
object : TencentLocationListener {
override fun onLocationChanged(p0: TencentLocation?, p1: Int, p2: String?) {
if (mfrom == null && p0 != null) {
mfrom = NaviPoi(p0.latitude, p0.longitude)
// 开始算路
calculateRoute();
}
Log.d(TAG, "onLocationChanged:${p0?.toString()}")
}
override fun onStatusUpdate(p0: String?, p1: Int, p2: String?) {
Log.d(TAG, "onStatusUpdate:${p0?.toString()}")
}
override fun onGnssInfoChanged(p0: Any?) {
Log.d(TAG, "onGnssInfoChanged:${p0?.toString()}")
}
override fun onNmeaMsgChanged(p0: String?) {
Log.d(TAG, "onNmeaMsgChanged:${p0?.toString()}")
}
},
Looper.getMainLooper()
)
// mLocationManager?.requestSingleFreshLocation(null, mLocationListener, Looper.getMainLooper());
}
}
private fun judgeLocationServerState(): Boolean {
try {
return Settings.Secure.getInt(this.contentResolver, Settings.Secure.LOCATION_MODE) > 1
} catch (e: Settings.SettingNotFoundException) {
e.printStackTrace()
}
return false
}
@AfterPermissionGranted(1)
fun requirePermission() {
val permissions = arrayOf<String>(
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION
)
val permissionsForQ = arrayOf<String>(
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_BACKGROUND_LOCATION
)
if (if (Build.VERSION.SDK_INT >= 29) EasyPermissions.hasPermissions(
this,
*permissionsForQ
) else EasyPermissions.hasPermissions(this, *permissions)
) {
showToast("权限OK")
} else {
var ps = if (Build.VERSION.SDK_INT >= 29) {
permissionsForQ
} else {
permissions
}
EasyPermissions.requestPermissions(
this, "需要权限",
1,
*ps
)
}
}
fun calculateRoute() {
var rideOption = RideRouteSearchOptions()
rideOption.type(1); // 0:自行车,1:电动车
try {
mRideManager?.searchRoute(
mfrom,
mDest,
rideOption,
object : TencentRouteSearchCallback {
override fun onCalcRouteSuccess(p0: CalcRouteResult?) {
Log.d(TAG, "onCalcRouteSuccess: $p0")
}
override fun onCalcRouteFailure(p0: CalcRouteResult?) {
Log.d(TAG, "onCalcRouteFailure: $p0")
runOnUiThread {
showToast("算路失败!!")
}
}
override fun onRouteSearchFailure(p0: Int, p1: String?) {
Log.d(TAG, "onRouteSearchFailure: $p0")
}
override fun onRouteSearchSuccess(p0: ArrayList<RouteData>?) {
Log.d(TAG, "onRouteSearchSuccess: $p0")
runOnUiThread {
showToast("算路成功!!")
}
if (p0 != null) {
// val polylineOptions = PolylineOptions()
// .addAll(p0[0].routePoints) // 折线设置圆形线头
// .lineCap(true) // 纹理颜色
// .color(PolylineOptions.Colors.GRAYBLUE)
// .width(25f)
//
// mRideNaviView?.map?.addPolyline(polylineOptions)
startNavi();
}
// 开始导航
}
});
} catch (e: Exception) {
Log.e(TAG, "calculateRoute: ${e.toString()}")
}
}
fun startNavi() {
initNaviView();
mRideManager?.setInternalTtsEnabled(true); // 开启播报
mRideManager?.addTencentNaviListener(mRideNaviView);
// 开始模拟导航
try {
// mRideManager?.startSimulateNavi(0);
mRideManager?.startNavi(0)
} catch (e: Exception) {
Log.e(TAG, "startNavi: $e", )
}
}
fun stopNavi() {
if (mRideManager != null) {
mRideManager?.stopNavi();
clearNaviView()
mRideManager?.removeTencentNaviListener(mRideNaviView)
}
}
fun initNaviView() {
if (mRideManager == null)
return;
// 导航面板
mRideNaviView?.setNaviPanelEnabled(true);
mRideNaviView?.setNaviPanelEnabled(true);
// 开启默认UI
var topNaviInfo = mRideNaviView?.showNaviInfoPanel();
topNaviInfo?.setOnNaviInfoListener(onNaviInfoListener);
}
fun clearNaviView() {
// 路线元素
mRideNaviView?.clearAllRouteUI();
mRideNaviView?.setNaviPanelEnabled(false);
// 默认ui
mRideNaviView?.hideNaviInfoPanel();
}
fun showToast(msg: String) {
Toast.makeText(this, msg, Toast.LENGTH_LONG).show()
}
override fun onDestroy() {
super.onDestroy()
if (mRideNaviView != null) {
mRideNaviView?.onDestroy();
}
stopNavi()
}
override fun onStart() {
super.onStart();
if (mRideNaviView != null) {
mRideNaviView?.onStart();
}
}
override fun onRestart() {
super.onRestart();
if (mRideNaviView != null) {
mRideNaviView?.onRestart();
}
}
override fun onResume() {
super.onResume();
if (mRideNaviView != null) {
mRideNaviView?.onResume();
}
}
override fun onPause() {
super.onPause();
if (mRideNaviView != null) {
mRideNaviView?.onPause();
}
}
}