当前位置: 首页 > news >正文

uniapp小程序实现地图多个标记点

实现效果图:

步骤一: 引入uniapp自带的地图组件,设置地图样式宽高

<map id="myMap" class="map-view" :longitude="longitude" :latitude="latitude"
                :scale="scale" :markers="markers" @markertap="handleMarkerTap" :circles="circles" :show-location="true"
                @callouttap="handleCalloutTap"></map>

步骤二:data中定义地图的相关参数

//以当前位置坐标为圆心
                circles: [{
                    latitude: 39.908580, // 圆心纬度
                    longitude: 116.397428, // 圆心经度
                    color: '#2A5CAA33', // 圆颜色
                    fillColor: '#2A5CAA33', // 圆内填充颜色,可选
                    radius: 2000, // 圆半径,单位m
                }],
                longitude: 116.397428, // 示例经纬度(先写死一个,否则标记点不显示),可根据实际定位获取
                latitude: 39.908580, // 示例经纬度(先写死一个,否则标记点不显示),可根据实际定位获取
                scale: 14,//地图缩放
                markers: [], //标记点
                location: "", 

步骤三: 获取当前位置坐标并动态设置地图相关参数

//获取当前位置坐标
            chooseLocation() {
                const that = this
                uni.getLocation({
                    type: 'wgs84', // 返回可以用于uni.openLocation的经纬度,默认为wgs84的gps坐标
                    // 是否解析地址信息(仅App平台支持(安卓需指定 type 为 gcj02 并配置三方定位SDK))
                    geocode: true,
                    success: function(res) {
                        that.longitude = res.longitude
                        that.latitude = res.latitude
                        that.circles[0].latitude = res.latitude
                        that.circles[0].longitude = res.longitude
                        that.location = res.longitude + ',' + res.latitude
                    }
                });
            }, 

步骤四:从接口中获取列表参数设置标记点 

        async getList() {
                uni.showLoading({
                    title: "加载中",
                    mask: true, //是否显示透明蒙层,防止触摸穿透
                })
                //写接口请求
                if (res.data.code == 1) {
                    const {
                        list,
                        count
                    } = res.data.data
                    this.triggered = false;
                    this.isfreshing = false;

                    //处理地图标记点
                    const mapMarks = list.map(item => {
                        return {
                            id: Number(item.id),
                            longitude: item.location.split(',')[0],
                            latitude: item.location.split(',')[1],
                            title: item.title,
                            iconPath: '/static/home/address_map.png', // 可自定义标记点(需要注意,必须写成绝对路径,相对路径不生效)
                            width: 18, //自定义图标宽
                            height: 18, //自定义图标高
                            anchor: {
                                x: 0.5,
                                y: 1
                            }, // 锚点位置,使标记点底部中心对准坐标点
                            zIndex: 100,
                            //自定义坐标点内容展示
                            callout: {
                                content: `${item.area.split('/')[2]}|${item.month_money}起|${item.square}m²`,
                                color: '#ffffff',
                                bgColor: '#2A5CAA',
                                padding: 6,
                                borderRadius: 6,
                                display: 'ALWAYS',
                                textAlign: 'center',
                                fontSize: 12
                            }
                        }
                    })

                    this.markers = [...this.markers, ...mapMarks]
                    this.datatList = [...this.datatList, ...list]
                    if (this.datatList?.length < count) {
                        this.hasmore = true
                        this.status = 'more'
                    } else {
                        this.hasmore = false
                        this.status = 'noMore'
                    }
                    uni.hideLoading()
                } else {
                    uni.hideLoading()
                }
            },

根据经纬度导航功能: 

//点击导航 
            handleNavigation(item) {
                //地图导航
                const arr = item.location.split(',')
                uni.openLocation({
                    latitude: arr[1] * 1,//必传
                    longitude: arr[0] * 1,//必传
                    name: item.address,
                    success: function() {
                        console.log('success');
                    }
                });
            }, 

完整代码: 

<template><view class="common_page_min"><Navbar title="地图找房" /><view class="search-page"><!-- 地图区域 --><map id="myMap" class="map-view" :longitude="longitude" :latitude="latitude" :scale="scale":markers="markers" @markertap="handleMarkerTap" :circles="circles" :show-location="true"@callouttap="handleCalloutTap"></map><!-- 房源列表 --><scroll-view class="house-list container mt16" v-if="datatList&&datatList.length>0" scroll-y="true"@scrolltolower="lower" lower-threshold="50" refresher-enabled :refresher-triggered="triggered"@refresherrefresh="onRefresh"><view class="map-house-item" v-for="item in datatList" :key="item.id"><!-- 写列表样式 --></view><!-- 底部加载更多 --><uni-load-more :status="status"></uni-load-more><view style="height: 60px;"></view></scroll-view></view></view>
</template><script>import {getBusinessListApi,getAreasListApi} from "@/request/api.js";export default {data() {return {//以当前位置坐标为圆心circles: [{latitude: 39.908580, // 圆心纬度longitude: 116.397428, // 圆心经度color: '#2A5CAA33', // 圆颜色fillColor: '#2A5CAA33', // 圆内填充颜色,可选radius: 2000, // 圆半径,单位m}],longitude: 116.397428, // 示例经纬度(先写死一个,否则标记点不显示),可根据实际定位获取latitude: 39.908580, // 示例经纬度(先写死一个,否则标记点不显示),可根据实际定位获取scale: 14, //地图缩放markers: [], //标记点location: "",// 房源列表数据datatList: [],currentPage: 1, // 当前页pageSize: 10, // 每页数据量status: 'more', // 加载状态,'more' 表示有更多数据,'loading' 表示加载中,'noMore' 表示没有更多数据hasmore: true,viewMode: 'map', // 'map' 或 'list'triggered: false,isfreshing: false,};},onShow() {},onLoad(options) {this.viewMode = options.viewMode || 'list'this.chooseLocation()},methods: {//获取当前位置坐标chooseLocation() {const that = thisuni.getLocation({type: 'wgs84', // 返回可以用于uni.openLocation的经纬度,默认为wgs84的gps坐标// 是否解析地址信息(仅App平台支持(安卓需指定 type 为 gcj02 并配置三方定位SDK))geocode: true,success: function(res) {that.longitude = res.longitudethat.latitude = res.latitudethat.circles[0].latitude = res.latitudethat.circles[0].longitude = res.longitudethat.location = res.longitude + ',' + res.latitude//获取数据列表接口that.firstgetOrderList()}});},// 处理气泡点击事件handleCalloutTap(e) {console.log('气泡====', e);// const markerId = e.markerId;// const marker = this.markers.find(item => item.id === markerId);// if (marker) {// 	uni.showModal({// 		title: marker.title,// 		content: `你点击了${marker.title}的信息气泡`,// 		showCancel: false// 	});// }},// 标记点点击事件handleMarkerTap(e) {const markerId = e.markerId;// 可根据标记点 ID 做对应逻辑,比如定位到房源详情等console.log('点击了标记点', markerId);},//刷新firstgetOrderList() {this.currentPage = 1this.datatList = []this.getList()},//获取列表async getList() {uni.showLoading({title: "加载中",mask: true, //是否显示透明蒙层,防止触摸穿透})//写接口请求if (res.data.code == 1) {const {list,count} = res.data.datathis.triggered = false;this.isfreshing = false;//处理地图标记点const mapMarks = list.map(item => {return {id: Number(item.id),longitude: item.location.split(',')[0],latitude: item.location.split(',')[1],title: item.title,iconPath: '/static/home/address_map.png', // 可自定义标记点(需要注意,必须写成绝对路径,相对路径不生效)width: 18, //自定义图标宽height: 18, //自定义图标高anchor: {x: 0.5,y: 1}, // 锚点位置,使标记点底部中心对准坐标点zIndex: 100,//自定义坐标点内容展示callout: {content: `${item.area.split('/')[2]}|${item.month_money}起|${item.square}m²`,color: '#ffffff',bgColor: '#2A5CAA',padding: 6,borderRadius: 6,display: 'ALWAYS',textAlign: 'center',fontSize: 12}}})this.markers = [...this.markers, ...mapMarks]this.datatList = [...this.datatList, ...list]if (this.datatList?.length < count) {this.hasmore = truethis.status = 'more'} else {this.hasmore = falsethis.status = 'noMore'}uni.hideLoading()} else {uni.hideLoading()}},//点击导航 handleNavigation(item) {//地图导航const arr = item.location.split(',')uni.openLocation({latitude: arr[1] * 1,//必传longitude: arr[0] * 1,//必传name: item.address,success: function() {console.log('success');}});},lower(e) {this.status = 'loading'if (this.hasmore) {this.status = 'loading'this.currentPage++this.getList()} else {this.status = 'noMore'}},onRefresh() {console.log('下拉刷新===');if (!this.triggered) {if (this.isfreshing) return;this.isfreshing = true;if (!this.triggered) {this.triggered = true;}this.status = 'more' // 加载状态,'more' 表示有更多数据,'loading' 表示加载中,'noMore' 表示没有更多数据this.hasmore = truethis.firstgetOrderList()}},}};
</script>
<style lang="scss" scoped>.search-page {width: 100%;height: calc(100vh - 110px);overflow: hidden;display: flex;flex-direction: column;}.map-view {width: 750rpx;height: 500rpx; // 使用 rpx 适配不同设备}
</style>

更多地图相关功能api参考官网:map | uni-app官网 

http://www.lryc.cn/news/590314.html

相关文章:

  • 数据结构与算法学习(一)
  • Java大厂面试实录:从Spring Boot到AI微服务架构的全栈挑战
  • PyCharm高效入门指南大纲
  • 图机器学习(8)——经典监督图嵌入算法
  • 浅析BLE/MQTT协议的区别
  • Web3.0与元宇宙:重构数字文明的技术范式与社会变革
  • 创客匠人解析:系统化工具如何重构知识变现效率
  • AI Agent:重构智能边界的终极形态——从技术内核到未来图景全景解析
  • UDP和TCP的主要区别是什么?
  • 智能呼叫中心系统:重构客户服务的核心引擎
  • 【保姆级喂饭教程】Idea中配置类注释模板
  • C++---emplace_back与push_back
  • Java接口:小白如何初步认识Java接口?
  • C语言 个人总结1
  • 【SF顺丰】顺丰开放平台API对接(Java对接篇)
  • AI Agent开发学习系列 - langchain之LCEL(2):LCEL 链式表达解析
  • Nand2Tetris(计算机系统要素)学习笔记 Project 0
  • 单片机学习笔记.IIC通信协议(根据数据手册写IIC驱动程序,这里以普中开发板上的AT24C02为例)
  • 【深度学习基础】PyTorch中model.eval()与with torch.no_grad()以及detach的区别与联系?
  • 嵌入式学习-PyTorch(5)-day22
  • 人工智能时代下的数据新职业:新兴工作岗位版图研究
  • 智能体架构深度解构:一次用户请求的完整旅程
  • 第二十一 篇 PDF文档自动化:Python一键合并、分割、水印、提取与加密解密!你的PDF全能管家!
  • audiorecord 之 抢占优先级
  • rLLM:用于LLM Agent RL后训练的创新框架
  • ESP32 S3 基于 Arduino 实现局域网视频流传输全解析
  • Python从入门到高手9.2节-Python字典的操作方法
  • 多维动态规划题解——不同路径【LeetCode】记忆化搜索
  • NumPy 常用操作详解汇总和实战示例
  • 泰语OCR识别技术方案