uniapp的map组件,显示标记距离
时间: 2024-01-07 11:05:40 浏览: 196
Uniapp的map组件可以通过计算两个标记之间的距离来显示它们之间的距离。可以使用Haversine公式来计算两个地理位置之间的距离,该公式可以计算出两个经纬度之间的距离。以下是一个示例代码,可以根据需要进行修改:
```html
<template>
<view>
<map :polyline="polyline" :markers="markers"></map>
<view>{{distance}} km</view>
</view>
</template>
<script>
export default {
data() {
return {
polyline: [],
markers: [{
id: 1,
latitude: 39.90469,
longitude: 116.40717,
label: {
content: '北京市',
color: '#FF0000',
fontSize: 16,
borderRadius: 10,
bgColor: '#FFFFFF',
padding: 5
}
}, {
id: 2,
latitude: 31.23037,
longitude: 121.4737,
label: {
content: '上海市',
color: '#FF0000',
fontSize: 16,
borderRadius: 10,
bgColor: '#FFFFFF',
padding: 5
}
}],
distance: ''
}
},
methods: {
getDistance(lat1, lng1, lat2, lng2) {
const radLat1 = lat1 * Math.PI / 180.0;
const radLat2 = lat2 * Math.PI / 180.0;
const a = radLat1 - radLat2;
const b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
s = s * 6378.137;
s = Math.round(s * 10000) / 10000;
return s.toFixed(2);
},
updateDistance() {
const marker1 = this.markers[0];
const marker2 = this.markers[1];
this.distance = this.getDistance(marker1.latitude, marker1.longitude, marker2.latitude, marker2.longitude);
}
},
mounted() {
this.polyline = [{
points: [{
latitude: this.markers[0].latitude,
longitude: this.markers[0].longitude
}, {
latitude: this.markers[1].latitude,
longitude: this.markers[1].longitude
}],
color: "#FF0000DD",
width: 2,
dottedLine: true
}];
this.updateDistance();
}
}
</script>
```
在上面的代码中,我们使用了getDistance方法来计算两个标记之间的距离,并在页面上显示它。我们还使用了polyline属性来绘制标记之间的连线,并使用markers属性来设置标记的位置和标注信息。在mounted方法中,我们初始化了polyline和distance,并调用了updateDistance方法来计算并显示距离。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)