最近找到了一篇uni-app的地图解决方案精品文章,这里分享给大家,希望对大家有所帮助
前置条件:你需要阅读
https://uniapp.dcloud.io/component/map
先看图

事件监听-属性说明
这个表也是官方的
标红的是我用到的

使用
html
我这里用了“@markertap”点击标记点时触发事件, “@tap”点击地图时触发事件。
<template>
<view class="content">
<view class="text-area">
<text class="title">{{title}}</text>
</view>
<view class="page-body">
<view class="page-section page-section-gap map" style="width: 100%; height: 900rpx;">
<map style="width: 100%; height: 100%;" scale='15' :latitude="latitude" :longitude="longitude" :markers="covers" @markertap="markertap" @tap="tap" @updated="updated">
</map>
</view>
</view>
</view>
</template>
js
<script>
export default {
data() {
return {
title: '百度地图',
latitude: 34.7586,
longitude: 113.672307,
covers: []
}
},
onLoad() {
this.init();
},
methods: {
init() {
let that = this;
console.log("init()")
var data = [{
id: 1,
name: '雷军',
imgUrl:'../../static/user.png',
lat: "34.7586",
lng: "113.672307"
},{
id: 2,
name: '李彦宏',
imgUrl:'../../static/user.png',
lat: "34.763466",
lng: "113.686285"
},{
id: 3,
name: '马化腾',
imgUrl:'../../static/user.png',
lat: "34.763412",
lng: "113.680185"
}, ];
that.MapData(that,data)
},
MapData(that, data) {
console.log(data.length)
console.log(data)
let arrayData = [];
for (var i = 0; i < data.length; i++) {
arrayData.push({
id: data[i].id,
latitude: data[i].lat,
longitude: data[i].lng,
title: data[i].name,
iconPath:data[i].imgUrl,
width: 60,
height: 60,
callout: {
content: data[i].name,
color: '',
fontSize: 16,
borderRadius: 20,
bgColor: '',
padding: 6,
display: 'BYCLICK',
textAlign: 'left',
},
label: {
content: '',
color: '#ff6600',
fontSize: 16,
x: 0,
y: 0,
borderWidth: 1,
borderColor: '',
borderRadius: 10,
bgColor: 'red',
padding: 6,
textAlign: 'left',
},
anchor: {
x: .5,
y: 1
}
});
}
console.log(arrayData.length)
console.log(arrayData)
that.covers = arrayData;
},
markertap(e) {
console.log("===你点击了标记点===")
console.log("你点击的标记点ID是:" + e.detail.markerId)
},
tap(e){
console.log("===你点击了地图点===")
console.log(e)
},
updated(e){
console.log("===在地图渲染更新完成时触发===")
console.log(e)
}
}
}
</script>
说明:
其中标记点图片为什么是圆形的在你的项目跟目录找到App.vue,放入下面代码
<style>
img.csssprite {
border-radius: 50px !important;
border: 1px #c7c7c7 solid !important;
}
</style>
如果对您有所帮助,欢迎您点个关注,我会定时更新技术文档,大家一起讨论学习,一起进步。
