小程序使用map组件实现轨迹,定位,导航,定位点遮罩
map组件常用属性说明,
看着还要自己研究写 我们立志成为cv工程师 直接上代码!!!
<template>
<map id="map" class="map" :show-location="true" :longitude="centerLng" :latitude="centerLat" :scale="scale"
:markers="markers" :circles="circles" :polyline="polyline" @updated="handleMapUpdate">
</template>
:longitude="centerLng"
:latitude="centerLat"
:markers="markers"
:polyline="polyline"
data(){
return{
}
},
新增地图多个点需要显示
onReady() {
setTimeout(() => {
this._mapContext = uni.createMapContext("map", this);
this._mapContext.initMarkerCluster({
enableDefaultStyle: false,
zoomOnClick: true,
gridSize: 1000,
complete(res) {
console.log('initMarkerCluster', res)
}
});
this._mapContext.on("markerClusterCreate", (e) => {
console.log("markerClusterCreate", e);
});
this.addMarkers();
}, 1500)
},
async addMarkers() {
uni.showLoading({
title: '加载中'
});
let that = this
await this.handleGetMapList()
await this.handleGetPhoenCore()
const markers = []
this.positions.forEach((p, i) => {
markers.push(
Object.assign({}, {
id: Number(i + 1),
iconPath: p.state == 1 ? that.img1 : that.img2,
width: 40,
height: 40,
bgColor: 'white',
zIndex: Number(199),
callout: {
id: Number(i + 1),
display: 'ALWAYS',
width: 40,
height: 40,
padding: that.isIOS ? 2.1 : 5,
borderRadius: 25,
bgColor: p.state == 1 ? '#00c187' : '#cdcdcd',
color: '#ffffff',
border: '1px solid #ffffff',
anchorX: 9,
anchorY: that.isIOS ? 13 : 11,
textAlign: "center",
content: JSON.stringify(p.high_voltage_counts)
},
label: {
width: 18,
height: that.isIOS ? 18 : 22,
borderRadius: 25,
bgColor: p.state == 1 ? '#00c187' : '#cdcdcd',
color: '#fff',
border: '1px solid #fff',
anchorX: -14,
anchorY: that.isIOS ? -45 : -52,
content: JSON.stringify(p.low_voltage_counts)
}
}, p)
)
})
that.mapList = markers
this._mapContext.addMarkers({
markers,
clear: false,
complete(res) {
console.log('addMarkers', res)
}
})
},
handleGetPhoenCore() {
uni.getSystemInfo({
success: (res) => {
console.log('获取手机内核', res)
console.log(res.osName == 'ios')
if (res.osName == 'ios') {
this.isIOS = true
console.log(this.isIOS)
}
}
})
},
1,定位遮蔽罩
<script>
handleMapUpdate(e) {
if (e.type === 'end' && e.causedBy === 'scale') {
this.circles = [];
this.markers = [];
this.polyline = [];
this.drawMarkers();
this.drawPolyline();
}
}
drawMarkers() {
console.log(this.centerLng, '经纬度')
console.log(this.centerLat, '经纬度')
this.markers.push({
id: 1,
longitude: this.centerLng,
latitude: this.centerLat,
title: '中心点',
iconPath: '/static/image/mark.png',
width: 30 + 'rpx',
height: 30 + 'rpx',
});
},
drawMarkers() {
this.markers.push({
id: 2,
longitude: this.polyline[0].longitude,
latitude: this.polyline[0].latitude,
title: '起点',
iconPath: '/static/image/marpq.png',
width: 80 + 'rpx',
height: 80 + 'rpx',
});
const lastIndex = this.polyline.length - 1;
this.markers.push({
id: 3,
longitude: this.polyline[lastIndex].longitude,
latitude: this.polyline[lastIndex].latitude,
title: '终点',
iconPath: '/static/image/markz.png',
width: 80 + 'rpx',
height: 80 + 'rpx',
});
},
drawCircle() {
uni.createSelectorQuery()
.select('#map')
.boundingClientRect(async (res) => {
const centerX = res.width / 2;
const centerY = res.height / 2;
const circle = {
latitude: this.centerLat,
longitude: this.centerLng,
color: '#54C3FF',
fillColor: '#58d0eb6a',
borderColor: '#58d0eb6a',
borderWidth: 0,
radius: Math.min(res.width, res.height) / 4,
strokeWidth: 0
};
this.circles.push(circle);
this.markers[0].left = centerX - 15;
this.markers[0].top = centerY - 15;
})
.exec();
},
</script>
2,上面二步奏我没传入经纬度 在地图上展示如下
3,轨迹
handleMapUpdate(e) {
if (e.type === 'end' && e.causedBy === 'scale') {
this.circles = [];
this.markers = [];
this.polyline = [];
this.drawMarkers();
this.drawPolyline();
}
}
drawPolyline() {
const polyline = {
points: JSON.parse(JSON.stringify(this.polyline)),
color: '#ff0000',
width: 2
};
this.polyline.push(polyline);
console.log(this.polyline)
},
导航
toMapAPP() {
if (that.centerLat != '' && that.centerLng != '') {
let that = this
uni.getSetting({
success: (res) => {
if (!res.authSetting['scope.userLocation']) {
uni.authorize({
scope: 'scope.userLocation',
success: () => {
const latitudes = that.centerLat
const longitudes = that.centerLng
const address = that.address
uni.openLocation({
latitude: parseInt(latitudes),
longitude: parseInt(longitudes),
name: address,
scale: 12,
success() {
console.log('success')
}
})
},
fail(error) {
console.log('拒绝授权', error)
uni.showModal({
title: '提示',
content: '若点击不授权,将无法使用位置功能',
cancelText: '不授权',
cancelColor: '#999',
confirmText: '授权',
confirmColor: '#f94218',
success(res) {
console.log(res)
if (res.confirm) {
uni.openSetting({
success(res) {
console.log(res
.authSetting)
}
})
} else if (res.cancel) {
console.log('用户点击不授权')
}
}
})
}
})
} else {
console.log('有授权');
console.log(that.centerLat, that.centerLng)
uni.getLocation({
type: 'wgs84',
success: (res) => {
const latitudes = that.centerLat
const longitudes = that.centerLng
const address = that.address
uni.openLocation({
latitude: parseInt(latitudes),
longitude: parseInt(longitudes),
name: address,
scale: 12,
success: (res) => {
console.log('success', )
},
fail: (err) => {
console.log('err', err)
}
})
},
fail(error) {
uni.showToast({
title: '请勿频繁调用!',
icon: 'none',
})
console.log('失败', error)
}
})
}
}
})
} else {
uni.showToast({
title: '暂无信息',
icon: 'none',
})
}
},