先看效果

地图中的标记点 可以动态变化自定义的图片与内容

原理

通过map组件中的 markers 属性 设置标记点 用于在地图上显示标记的位置

map组件所在uniapp官网位置:map-uni-app https://uniapp.dcloud.io/component/map

1、想要动态改变标记点图标 必须把默认的标记点覆盖,用到了markers参数下的 iconPath设置标记点的图标,如果需求只需要简单的动态改变标记点的图标到这一步就结束了,但是如果需要动态改变标记点图标及内容(动态显示文字)就需要看下一步!
2、把iconPath设置的图片改用一张1*1像素的透明背景图 png格式的。

3、markers 列表中的 customCallout 设置自定义气泡窗口 display: "ALWAYS"总数显示,并在标签内配置 slot="callout"这里注意的是 要比map组件层级高 必须使用 cover-view 或者 cover-image 标签;

<!-- 自定义窗口 -->
<cover-view slot="callout"><template v-for="(item,index) in markers"><cover-view :marker-id="item.id" :key='index'><cover-view class="position-relative" style="width: 92rpx; height: 78rpx;"><cover-image class="position-absolute" style="width: 92rpx; height: 78rpx;" :src='item.options.iconPath'></cover-image><cover-view style="top: 13rpx; left: 45rpx; width: 30rpx; height: 30rpx;"class="border bg-white rounded-circle flex align-center justify-center position-absolute"><cover-view>{{item.options.labelName}}</cover-view></cover-view></cover-view></cover-view></template></cover-view>

4、map标签上使用 @callouttap事件 点击标记点对应的气泡时触发,这样就可以动态获取标记点信息了。

<map id="map" ref="map" style="height: 870rpx; width: 750rpx;" :latitude="latitude" :longitude="longitude" :markers="markers" :enable-building='true'
:show-location='true' :circles='circles' @markertap='markertap' @callouttap='callouttap'></map>

其中、第二步中使用透明背景图把默认标记点隐藏,第三步把自定义气泡弹窗改为标记点,偏移量自行微调到原标记点上方,第四步,获取到点击标记点的回调。这样,就可以实现动态改变标记点图标及内容了。

附上代码(样式缺失,自行修改)

<template><view>
<!--         <nav-bar bgColor="#4f8fef" fontColor="#fff" title="地图" :titleCenter='true'></nav-bar><free-scroll-main :scrollView='false' :showTabber="false" scrollViewStyle="background-color: #fafaf9;"></free-scroll-main> --><view class="bg-dark" style="height: 870rpx; width: 750rpx;"><map id="map" ref="map" style="height: 870rpx; width: 750rpx;" :latitude="latitude" :longitude="longitude" :markers="markers" :enable-building='true':show-location='true' :circles='circles' @markertap='markertap' @callouttap='callouttap'><!-- 自定义窗口 --><cover-view slot="callout"><template v-for="(item,index) in markers"><cover-view :marker-id="item.id" :key='index'><cover-view class="position-relative" style="width: 92rpx; height: 78rpx;"><cover-image class="position-absolute" style="width: 92rpx; height: 78rpx;" :src='item.options.iconPath'></cover-image><cover-view style="top: 13rpx; left: 45rpx; width: 30rpx; height: 30rpx;"class="border bg-white rounded-circle flex align-center justify-center position-absolute"><cover-view>{{item.options.labelName}}</cover-view></cover-view></cover-view></cover-view></template></cover-view></map></view><view class="mt-5 flex align-center justify-center"><view class="position-relative border bg-white rounded-10" style=" width: 650rpx; height: 220rpx;"><view :style="getBgColor" class="position-absolute rounded-circle flex align-center justify-center"style="width: 60rpx; height: 60rpx; top: -30rpx; left: 30rpx;"><text class="text-white" style="font-size: 24rpx;">{{targetInfo.labelName}}</text></view><view class=" pt-3 px-3 flex align-center justify-between"><view class="flex flex-column"><text class="font-md font-weight-bolder">{{targetInfo.locationName}}</text><text class="font-sm pt-1">{{targetInfo.address}}</text><text class="font-sm py-1">{{targetInfo.city}}</text><text class="font-sm">{{targetInfo.phoneNumber}}</text></view><view class="flex flex-column"><u-button class="btn mb-4" type="primary">预约</u-button><u-button @click="openMap" class="btn" type="primary">导航</u-button></view></view></view></view></view>
</template><script>import Map from '@/common/js/openMap.js'export default {components: {},data() {return {// 圆球颜色color: ['#0797e4', '#2be4ae'],// 定位维度latitude: 22.26666,// 定位经度longitude: 113.54342,// 点击选中idcheckedId: 0,// 选中marker列表下标markerIndex: -1,// 点击获取到的标点信息targetInfo: {labelName: 'A',locationName: '嘉苑',address: '情侣路公交亭',city: '珠海',phoneNumber: '(0123)119119119',latitude: 22.26666,longitude: 113.54342,},// 标点列表markers: [//    {//     id: 1, //标记点id//    clusterId: 1, //自定义点聚合簇效果时使用//  title: '选中',//    latitude: 22.26666, //维度//  longitude: 113.54342, //经度//    alpha: 1, //透明度 0-1//   iconPath: '/static/images/map/none.png',//    options: {//        checked: true,//        iconPath: '/static/images/map/select.png',//      labelName: 'A',//         locationName: '嘉苑',//         address: '高新区大学路101号',//      city: '珠海',//         phoneNumber: '(0123)119119119'//  },//    // 自定义窗口//  customCallout: {//      anchorX: -1,//      anchorY: 46,//      display: "ALWAYS"//   },// },],// 是否显示圆circles: [{latitude: 22.26666,longitude: 113.54342,color: '#2979ffcc',fillColor: '#2979ff61',strokeWidth: 1,radius: 500,}],// 控件controls: []}},watch: {// 监听选中下标 改变图片 markerIndex(val, oldVal) {this.markers[val].options.iconPath = '/static/images/map/none.png'this.markers[val].options.iconPath = '/static/images/map/select.png';if (this.markers[oldVal]) {this.markers[oldVal].options.iconPath = '/static/images/map/noselect.png';}console.log(val, oldVal);}},computed: {getBgColor() {return `background-image: linear-gradient(90deg, ${this.color[0]}, ${this.color[1]});`}},created() {this.isGetLocation();},methods: {// 测试ceshi() {for (let i = 0; i < 5; i++) {let obj = {id: 1, //标记点idclusterId: 1, //自定义点聚合簇效果时使用title: '选中',latitude: 22.26666, //维度longitude: 113.54342, //经度alpha: 1, //透明度 0-1iconPath: '/static/images/map/none.png',options: {iconPath: '/static/images/map/noselect.png',labelName: 'A',locationName: '嘉苑',address: '高新区大学路101号',city: '珠海',phoneNumber: '(0123)119119119'},// 自定义窗口customCallout: {anchorX: -1,anchorY: 46,display: "ALWAYS"},}let start = this.markers.length;obj.id = start;obj.clusterId = start;obj.options.labelName = String.fromCharCode(65 + start) // Athis.markers.push(obj);}this.markers.forEach((item, index) => {let timer = setTimeout(() => {let suiji = Math.round(Math.random() * 10) * 0.0003;if (Math.round(Math.random() * 5) > 2) {suiji = -suiji;}if (index % 2 != 0) {this.markers[index].longitude = this.longitude + suiji;this.markers[index].latitude = this.latitude + suiji;} else {this.markers[index].longitude = this.longitude + suiji;this.markers[index].latitude = this.latitude - suiji;}console.log(suiji);clearTimeout(timer);timer = null;}, 50 * index)})setTimeout(() => {this.getTargetInfo(this.markers[0]);}, 1500)},getTargetInfo(obj) {this.targetInfo.labelName = obj.options.labelName;this.targetInfo.locationName = obj.options.locationName;this.targetInfo.address = obj.options.address;this.targetInfo.phoneNumber = obj.options.phoneNumber;this.targetInfo.longitude = obj.longitude;this.targetInfo.latitude = obj.latitude;},// 点击标记点markertap(e) {console.log(e, '标记点e');},// 点击自定义气泡callouttap(e) {this.checkedId = e.detail.markerId;let markerId = e.detail.markerId;for (let i = 0; i < this.markers.length; i++) {if (this.markers[i].id == markerId) {this.markerIndex = i;break;}}if (this.markerIndex != -1) {this.getTargetInfo(this.markers[this.markerIndex]);} else {uni.showToast({title: '暂未该设备信息',})}},// 打开地图导航openMap() {console.log('uni.getSystemInfoSync().platform', uni.getSystemInfoSync().platform);console.log(this.targetInfo.latitude, this.targetInfo.longitude);// Map.openMap(this.targetInfo.latitude, this.targetInfo.longitude, this.targetInfo.locationName, 'wgs84')var options = {origin: { //导航起点坐标和名称,如果不传则起点为当前位置latitude: this.latitude,longitude: this.longitude,name: "起点"},destination: { //导航终点点坐标和名称latitude: this.targetInfo.latitude,longitude: this.targetInfo.longitude,name: this.targetInfo.locationName},// #ifdef MP-WEIXINmapId: "map",// #endifmode: "drive" //导航方式 公交:bus驾车:drive(默认),步行:walk,骑行:bike;}//路线规划Map.routePlan(options, "gcj02")//驾车导航,打开地图直接开启导航,只需要传入options.destination终点信息Map.navigation(options, "gcj02")},// 获取定位信息getLocationInfo() {uni.getLocation({type: 'gcj02', //gcj02国测局坐标success: (res) => {console.log('当前位置的经度:' + res.longitude);console.log('当前位置的纬度:' + res.latitude);this.longitude = res.longitude;this.latitude = res.latitude;this.circles[0].longitude = res.longitude;this.circles[0].latitude = res.latitude;this.ceshi();}});},// 获取定位授权getAuthorizeInfo(a = "scope.userLocation") { //1. uniapp弹窗弹出获取授权(地理,个人微信信息等授权信息)弹窗var _this = this;uni.authorize({scope: a,success() { //1.1 允许授权_this.getLocationInfo();},fail() { //1.2 拒绝授权console.log("你拒绝了授权,无法获得周边信息")}})},// 查看是否已经授权定位isGetLocation(a = "scope.userLocation") { // 3. 检查当前是否已经授权访问scope属性,var _this = this;// #ifdef MPuni.getSetting({success(res) {if (!res.authSetting[a]) { //3.1 每次进入程序判断当前是否获得授权,如果没有就去获得授权,如果获得授权,就直接获取当前地理位置_this.getAuthorizeInfo()} else {_this.getLocationInfo()}}});// #endif},}}
</script>
<style scoped lang="scss">.btn {&/deep/.u-btn {width: 148rpx;height: 56rpx;}}
</style><style>@import '@/common/free.css';@import '@/common/common.css';
</style>

uniapp 使用map组件 动态自定义标记点图标及内容文字相关推荐

  1. uniapp中map组件动态加载marks标记

    之前在uniapp项目中用到地图都是用h5的方式实现的,比如用高德地图webapi可以不收uniapp的限制,uniapp中vue页面中map组件功能太少,nvue中css写的有蛋疼. 言归正传,ma ...

  2. uni-app使用map组件开发map地图,获取后台返回经纬度进行标点

    unia-pp实现map组件,获取后台返回经纬度进行标点 官方文档使用步骤 代码示例 官方文档使用步骤 使用uniapp是因为它是一个使用vue.js开发所有前端应用的框架,开发者编写一套代码,可以发 ...

  3. vue中element ui 中tree组件怎么自定义前缀图标呢?

    一 问题 饿了么ui 默认的图标样式是: 1. 一个箭头, 展开自动顺时针旋转90°, 以上的条件是该节点有子节点, 2. 如果是没有子节点的节点, 是默认空白图标的(这里我认为他不是没有, 而是有占 ...

  4. uniapp nvue map组件

    Map组件功能使用 uni.getLocation 实时定位功能 设置地图宽高 markers 设置地图控件ioc 和气泡 @regionchangeFn 拖动地图 从新定位 获取地址 mapSear ...

  5. uniapp 微信小程序地图map组件样式自定义

    1.第一步 进入 微信小程序JavaScript SDK | 腾讯位置服务 2. 进入应用管理 > 我的应用 > 右上角 创建应用 3.创建完应用,进入下面的个性化地图 > 样式选择 ...

  6. 【微信小程序】 map 组件 callout 自定义气泡 (实实在在的讲解)

    自定义气泡为什么出不来呢? 主要注意以下问题: 首先这里的 customCallout 是作为一个属性存在的,那么自然要在 WXML 中有所体现,主要体现在 markers 对象中的数据结构. 需要保 ...

  7. 实现flex LinkBar 组件 动态切换ico图标

    <?xml version="1.0" encoding="utf-8"?> <s:WindowedApplication xmlns:fx= ...

  8. uniapp使用map地图组件

    本人所在的公司最近在做共享电动车的项目,打开该应用看到的就是一副地图,可以获取附近共享电动车的地址.状态等信息,开发这个应用程序还需要在地图上规划路线.动态显示轨迹.轨迹回放等功能.共享开发的项目功能 ...

  9. Naive 组件库 动态渲染icon图标

    使用Naive组件库默认你已经安装了对应的icon图标库 没有安装的参考链接安装 点击传送 Naive组件动态渲染Icon图标 主要是使用 component Vue内置组件来渲染对应的icon组件, ...

最新文章

  1. 设计模式——工厂方法模式和抽象工厂模式
  2. mysql忘记密码的处理方法
  3. P2184 贪婪大陆
  4. macbook所有型号大全_苹果笔记本型号大全
  5. jenkins配置记录(2)--代码发布流程
  6. vue项目:this.function()中关于:this指针失效的问题
  7. 961计算机组成原理,2017年华中科技大学附属协和医院961计算机组成原理考研强化模拟题...
  8. ajax req.getsession() 获取不到_[AJAX] -AJAX,你要学的,都在这儿(案例小剧场-搜索框提示(经典))...
  9. Java回调函数实现案例
  10. Matlab实现Hermite插值多项式
  11. pythonopencv直方图均衡化_OpenCV-Python教程(10、直方图均衡化)
  12. 计算机硬盘分区类型,常见磁盘分区格式的区别是什么?
  13. 使用 Hexo 快速免费搭建个人网站
  14. 基于SmartThreadPool线程池技术实现多任务批量处理
  15. 数据集:人群行为识别数据库总结
  16. GVIM/VIM常用快捷操作(更新中)
  17. 厦门雅迅网络 Telematics(汽车远程信息服务)的中国期待
  18. android 雷达坐标系,Android Path之绘制雷达图的技巧
  19. 情人节程序员用HTML网页表白【粉红爱心表白】 HTML5七夕情人节表白网页源码 HTML+CSS+JavaScript
  20. Android(SpreadTurm)多语言版本电话录音系统报错

热门文章

  1. 数据库插入微信表情错误
  2. JAVA 利用多方法查找质数(素数)
  3. C语言用for循环求最大公约数,C语言循环结构 -C语言求最大公约数
  4. w10系统老是自动息屏_win10关闭屏幕后黑屏无法唤醒?你一定要学会这几招解决方法...
  5. 图的匹配---图论笔记
  6. PHP中=是什么意思
  7. 微软语音包的安装及使用 Microsoft Speech SDK
  8. 高等数学期末总复习DAY18.常数项级数、正项级数、交错级数、绝对收敛
  9. win2003封闭端口_Win 2003下添加LPT端口有妙招
  10. 央企招聘:中国航空工业集团直属公司2023秋季招聘