基础功能 最后更新时间: 2024年10月18日
使用导航SDK之前,需要在 config.json 文件中进行相关权限设置,确保导航功能可以正常使用。
第一步,配置module.json5
首先,声明权限
...
"requestPermissions": [
{
"name": "ohos.permission.APPROXIMATELY_LOCATION",
"reason": "$string:Harmony_navi_permission_reason",
"usedScene": {
"when": "always"
}
},
{
"name": "ohos.permission.LOCATION",
"reason": "$string:Harmony_navi_permission_reason",
"usedScene": {
"when": "always"
}
},
{
"name": "ohos.permission.LOCATION_IN_BACKGROUND",
"reason": "$string:Harmony_navi_permission_reason",
"usedScene": {
"when": "always"
}
},
{
"name": "ohos.permission.INTERNET",
"reason": "$string:Harmony_navi_permission_reason",
"usedScene": {
"when": "always"
}
},
{
"name": "ohos.permission.KEEP_BACKGROUND_RUNNING",
"reason": "$string:Harmony_navi_permission_reason",
"usedScene": {
"when": "always"
}
}
]
...
第二步,向工程中添加导航开发包
从ohpm仓库获取导航包,依次添加依赖。
"dependencies": {
"@amap/amap_lbs_common": ">=1.1.0",
"@amap/amap_lbs_location": ">=1.1.0",
"@amap/amap_lbs_navi": ">=2.1.0"
}
第三步,使用示例
1
首先,创建导航单例,并设置Key
AMapNaviFactory.getAMapNaviInstance(getContext().getApplicationContext(), appkey)
2
然后,添加导航组件
build() {
NavDestination() {
AMapNaviComponent({
appCustomerConfig: {
mType: AmapNaviType.Driver, //引擎类型(驾车、骑行、步行)。当前支持驾车、货车、摩托车
//默认模拟导航
// mNaviType: NaviType.EMULATOR,
//实时导航
mNaviType: NaviType.GPS,
start: {
coordinate: this.startLatLng
}, //起点
end: {
coordinate: this.endLatLng
}, //终点
// wayPoints: this.wayPoints, //途经点
mRouteStrategy: 10, //设置默认规划路线策略
serviceAreaDetailsEnable: true, //服务区信息
goBack: () => {
this.goback()
},
}
})
}.title('导航组件')
.hideTitleBar(true)
.onBackPressed(() => {
this.goback()
return true
})
}
3
最后,添加导航监听,监听回调中开始算路、导航
let listener: IAMapNaviListener = {
onInitNaviFailure: this.onInitNaviFailure,
onInitNaviSuccess: this.onInitNaviSuccess,
onStartNavi: this.onStartNavi,
onTrafficStatusUpdate: this.onTrafficStatusUpdate,
onLocationChange: this.onLocationChange,
onGetNavigationTextAndType: this.onGetNavigationTextAndType,
onEndEmulatorNavi: this.onEndEmulatorNavi,
onArriveDestination: this.onArriveDestination,
onReCalculateRouteForYaw: this.onReCalculateRouteForYaw,
onReCalculateRouteForTrafficJam: this.onReCalculateRouteForTrafficJam,
onArrivedWayPoint: this.onArrivedWayPoint,
onGpsOpenStatus: this.onGpsOpenStatus,
onNaviInfoUpdate: this.onNaviInfoUpdate,
updateCameraInfo: this.updateCameraInfo,
updateIntervalCameraInfo: this.updateIntervalCameraInfo,
onServiceAreaUpdate: this.onServiceAreaUpdate,
showCross: this.showCross,
hideCross: this.hideCross,
showModeCross: this.showModeCross,
hideModeCross: this.hideModeCross,
showLaneInfo: this.showLaneInfo,
hideLaneInfo: this.hideLaneInfo,
notifyParallelRoad: this.notifyParallelRoad,
// OnUpdateTrafficFacility: this.OnUpdateTrafficFacility,
onPlayRing: this.onPlayRing,
onTTSIsPlaying: this.onTTSIsPlaying,
onCalculateRouteSuccess: this.onCalculateRouteSuccess,
onCalculateRouteFailureForResult: this.onCalculateRouteFailureForResult,
onNaviRouteNotify: this.onNaviRouteNotify,
onUpdateGPSSignalStrength: this.onUpdateGPSSignalStrength,
}
this.listener = listener
//添加导航事件回调监听。
this.naviInstance.addAMapNaviListener(this.listener)