记录下uniapp开发结合webview调用高德地图(路线规划,多路选择

本文介绍了如何在uniapp的web端开发中使用高德地图API,包括初始化地图、获取定位信息、规划并绘制路线等步骤。作者展示了如何设置地图参数、挂载安全密钥、获取定位以及绘制路线的详细代码,提供了完整的流程示例。
摘要由CSDN通过智能技术生成

2.web端开发

  1. web vue项目引入这个"@amap/amap-jsapi-loader": "^1.0.1"插件,
  2. 在你页面js中先挂载window上这个安全密钥
    window._AMapSecurityConfig = { securityJsCode: '生成的安全密钥', }
  3. 初始化地图:

async initMap() {
try {
this.AMap = await this.AMapLoader.load({
key: “你生成到的key”,
version:“2.0”,
plugins:[],
// resizeEnable: true
dragEnable: true,
})
this.mapInstance = await new this.AMap.Map(“mapContainer”,{ //设置地图容器id
viewMode:“3D”, //是否为3D地图模式
zoom: 15, // 缩放级别
pitch: 0, // 俯视角度
center: new this.AMap.LngLat(this.myLocation.longitude,this.myLocation.latitude), //初始化地图中心点位置
});
// 画用自定义点
this.selfMarker = await new this.AMap.Marker({
map: this.mapInstance,
position: [this.preLocation.longitude, this.preLocation.latitude],
content: this.$refs.selfMarkerRef, // 自定义地图锚点dom
anchor: new this.AMap.Pixel(0,0),
offset: new this.AMap.Pixel(-20,-20),
size: new this.AMap.Size(20, 20)
});
// 自己走过的线(先定义)
this.passedPolylineInstance = new this.AMap.Polyline({
map: this.mapInstance,
strokeColor: “#b6bcb2”, //线颜色
strokeWeight: 6, //线宽
});
Promise.all([this.AMap,this.mapInstance,this.selfMarker,this.passedPolylineInstance]).then(res=>{
this.getPositionByAmap()
// 路线规划
this.routePlan()
})
} catch (err) {

}
},

  1. 高德获取定位信息

// 获取定位
getPositionByAmap() {
if(this.AMap) {
this.AMap.plugin(‘AMap.Geolocation’, () => {
let geolocation = new this.AMap.Geolocation({
// enableHighAccuracy: true,//是否使用高精度定位,默认:true
noIpLocate: 3, // 禁止ip定位
maximumAge: 300, //定位结果缓存0毫秒,默认:0
convert: true, //自动偏移坐标,偏移后的坐标为高德坐标,默认࿱

  • 25
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值