小程序 uniapp 地图 自定义内容呈现,获取中心点,获取对角经纬度,首次获取对角经纬度

ops/2025/2/1 15:39:04/

前言

使用uniapp 小程序 使用地图,我使用的是uniapp原生的地图,实现根据坐标在地图上显示自定义内容,首次加载获取坐标对角经纬度,通过对角经纬度给后端,进行只显示当前屏幕内的自定义内容,在通过拖拽事件,加载范围内(对标坐标经纬度)显示需要加载的内容,以及中心点,地图层级。

在这里插入图片描述

部分代码解释

scale:地图缩放级别
markers:标记点	
callouttap:标记点点击事件
markertap:markers内容的点击事件,就是自定义内容的点击事件
regionchange:推拽事件
regionchange - begin :拖拽开始事件
regionchange - end:拖拽结束事件
this.mapContext = uni.createMapContext("map", this); // 创建地图上下文
that.mapContext.getCenterLocation: 获取中心点位置
that.mapContext.getRegion : 获取后获取对角坐标
that.mapContext.getScale : 获取缩放级别

全部代码

<template><view><!-- 搜索 --><view class="search-box"><view class="search"><text class="iconfont icon-sousuo-copy-copy"></text><input type="text" confirm-type="search" @confirm="searchInput" v-model="search" placeholder="请输入搜索内容"></view></view><map id="map" class="map" style="width: 100%; height: 100vh;" :scale="scale" :latitude="latitude":longitude="longitude" :markers="covers" @callouttap='callouttap' @markertap="mapAction"@regionchange="regionchange"></map></view>
</template><script>export default {data() {return {scale: 11,// 地图缩放级别id: 0, // 使用 marker点击事件 需要填写idtitle: 'map',// 地图中心点 为空 默认北京   latitude: 34.745982,longitude: 113.658233,covers: [],search: '',//搜索内容mapContext: '',//地图对象zuobiao: {},//拖拽坐标}},onLoad() {this.mapContext = uni.createMapContext("map", this); // 创建地图上下文this.getInitialFocusCoordinates(); // 获取首次屏幕对焦经纬度},mounted() {// this.mapContext = uni.createMapContext("map", this); // 创建地图上下文// this.getInitialFocusCoordinates(); // 获取首次屏幕对焦经纬度},// 下拉刷新onPullDownRefresh() {this.getInitialFocusCoordinates(); // 获取首次屏幕对焦经纬度uni.showToast({title: '刷新成功',icon: 'success',duration: 1000})uni.stopPullDownRefresh(); // 停止刷新	},methods: {// markers 内容的点击事件。callouttap(e) {console.log("e", JSON.parse(JSON.stringify(e)));const marker = this.covers.find(item => {return item.id == e.detail.markerId});// 导航跳转uni.openLocation({latitude: parseFloat(marker.latitude),longitude: parseFloat(marker.longitude),name: marker.callout.content,address: marker.address,success: function (res) {console.log('打开系统位置地图成功')},fail: function (error) {console.log(error)}})},// 获取首次屏幕对焦经纬度getInitialFocusCoordinates() {this.mapContext.getRegion({success: (res) => {const northeast = res.northeast; // 东北角const southwest = res.southwest; // 西南角const zuobiao = {northeast: northeast,southwest: southwest};this.zuobiao = [zuobiao.northeast, zuobiao.southwest];this.QiyeMap();console.log("this.首次对角", JSON.parse(JSON.stringify(this.zuobiao)));},fail: (err) => {console.error('获取对焦经纬度失败:', err);}});},// 企业地图数据async QiyeMap() {const res = await this.$axios("home/QiyeMap", {title: this.search,zuobiao: this.zuobiao,scale: this.scale,})console.log("企业地图数据", JSON.parse(JSON.stringify(res.data)));if (res.data.code == 0) {this.covers = res.data.lists.map(item => {return {id: item.id,latitude: item.lat,longitude: item.lon,address: item.address,// iconPath: '/static/images/map.png',width: 22,height: 22,callout: {content: item.title,display: 'ALWAYS',color: '#ffffff',fontSize: 12,borderRadius: 4,bgColor: '#000',padding: '5',}}});}},// 地图标记点点击事件mapAction(e) {const marker = this.covers.find(item => {return item.id == e.detail.markerId});// 导航跳转uni.openLocation({latitude: parseFloat(marker.latitude),longitude: parseFloat(marker.longitude),name: marker.callout.content,address: marker.address,success: function (res) {console.log('打开系统位置地图成功')},fail: function (error) {console.log(error)}})},searchInput(e) {this.search = e.detail.value;this.QiyeMap();},//监听地图拖拽regionchange(data) {console.log("拖拽", JSON.parse(JSON.stringify(data)));// this.mapContext = uni.createMapContext("map", this);//拖拽地图const that = this;if (data.type == "end") {// 获取拖拽后的中心点that.mapContext.getCenterLocation({success: function (res) {that.latitude = res.latitude;that.longitude = res.longitude;// 获取后获取对角坐标that.mapContext.getRegion({success: (res) => {const zuobiao = {northeast: res.northeast,southwest: res.southwest};that.zuobiao = [zuobiao.northeast, zuobiao.southwest];// 获取缩放级别that.mapContext.getScale({success: (res) => {if (res.scale !== that.scale) {that.scale = res.scale; // 更新缩放级别}}})that.QiyeMap();}})}});}},}}
</script><style lang="scss" scoped>
.search-box {background-color: white;// padding: 1rem;padding-bottom: .5rem;.search {width: 90%;margin: auto;display: flex;align-items: center;font-size: 14px;// border: 1px solid red;background-color: #f7f7f7;padding: 0.4rem;border-radius: 30px;.icon-sousuo-copy-copy {font-size: 14px;margin-right: 10px;margin-left: .5rem;}}}
</style>

http://www.ppmy.cn/ops/154785.html

相关文章

装饰SpringMVC的适配器实现响应自动包装

文章目录 1.common-tool-starter1.目录结构2.ResultWrapper.java 2.common-web-starter1.目录结构2.IgnoredResultWrapper.java 自定义注解&#xff0c;忽略对返回结果的自动包装3.ReturnValueHandlersDecorator.java 对适配器进行扩展的装饰器4.WebAutoConfiguration.java 将装…

SAP内向交货单详解

【SAP系统研究】 #SAP #交货单 #内向交货单 一、内向交货单的概念 内向交货单,Inbound Delivery,是SAP系统中用于管理外部供应商或内部工厂向公司发货的文档。它记录了货物从供应商到公司仓库或生产地点的运输和接收过程。 内向交货单的主要功能有: (1)货物接收:用于…

Java---猜数字游戏

本篇文章所实现的是Java经典的猜数字游戏 , 运用简单代码来实现基本功能 目录 一.题目要求 二.游戏准备 三.代码实现 一.题目要求 随机生成一个1-100之间的整数(可以自己设置区间&#xff09;&#xff0c;提示用户猜测&#xff0c;猜大提示"猜大了"&#xff0c;…

LeetCode 跳跃类问题的解题技巧:跳跃游戏与最少跳跃数

LeetCode 跳跃类问题的解题技巧&#xff1a;跳跃游戏与最少跳跃数 在 LeetCode 中&#xff0c;有一类经典的题目是关于跳跃的&#xff0c;例如 跳跃游戏 和 跳跃游戏 II。这些问题通常要求我们判断是否能从数组的起始位置跳跃到结束位置&#xff0c;或者求得最少跳跃次数。解决…

C++ list

list需知&#xff1a; list不会出现insert迭代器失效问题 链表插入不会影响原有数据相对位置&#xff0c;且不用扩容 但是erase会导致相对数据位置移动&#xff0c;所有其erase会导致迭代器失效 list排序效率很低 不建议使用 小规模数据量可以使用&#xff0c;比较方便 此外…

Github 2025-01-31Java开源项目日报 Top10

根据Github Trendings的统计,今日(2025-01-31统计)共有10个项目上榜。根据开发语言中项目的数量,汇总情况如下: 开发语言项目数量Java项目10C项目1Kotlin项目1Bazel:快速、可扩展的多语言构建系统 创建周期:3564 天开发语言:Java协议类型:Apache License 2.0Star数量:2…

字符串,集合

String 概述 java.lang.String 类代表字符串&#xff0c;Java 程序中的所有字符串文字(例如“abc”)都为此类的对象 注意&#xff1a;字符串的内容是不会发生改变的&#xff0c;它的对象在创建后不能被更改 String是Java定义好的一个类。定义在java.lang 包中&#xff0c;所…

DDD - 领域驱动设计分层架构:构建可演化的微服务架构

文章目录 引言1. 什么是DDD分层架构&#xff1f;1.1 DDD分层架构的演变1.2 四层架构的起源与问题1.3 依赖倒置和五层架构 2. DDD分层架构的核心层次2.1 用户接口层&#xff08;User Interface Layer&#xff09;2.2 应用层&#xff08;Application Layer&#xff09;2.3 领域层…