uniapp舆图导航及后台百度舆图回显(v2/v3版本)

打印 上一主题 下一主题

主题 1517|帖子 1517|积分 4551

百度舆图申请地点:控制台 | 百度舆图开放平台
结果:



1.后台

1.1申请百度舆图APi


1.2 引入百度舆图


  1. <script type="text/javascript" src="//api.map.baidu.com/api?v=3.0&ak=自己百度生气apikey"></script>
复制代码
1.3 v2组件

  1. <template>
  2.   <div>
  3.     <!-- 地图容器 -->
  4.     <div id="map-container" class="map-container"></div>
  5.     <!-- 结果展示区域 -->
  6.     <div class="result">
  7.       搜索:<el-input v-model="keyMap" placeholder="请输入地址" size="small" clearable></el-input>
  8.       <el-button type="primary" size="small" @click="searchAddress">搜索</el-button>
  9.     </div>
  10.   </div>
  11. </template>
  12. <script>
  13. export default {
  14.   data() {
  15.     return {
  16.       keyMap: '',
  17.       map: null,
  18.       marker: null,
  19.       geoc: null
  20.     };
  21.   },
  22.   methods: {
  23.     updatePositionAndAddress(point) {
  24.       this.$emit('update:marker-position', {
  25.         lat: point.lat,
  26.         lng: point.lng
  27.       });
  28.       this.geoc.getLocation(point, (rs) => {
  29.         if (rs) {
  30.           this.$emit('update:address', rs.address);
  31.         }
  32.       });
  33.     },
  34.     handleMapClick(e) {
  35.       if (this.marker) {
  36.         this.map.removeOverlay(this.marker);
  37.       }
  38.       this.marker = new BMap.Marker(e.point);
  39.       this.map.addOverlay(this.marker);
  40.       this.updatePositionAndAddress(e.point);
  41.     },
  42.     searchAddress() {
  43.       if (!this.keyMap) return;
  44.       const local = new BMap.LocalSearch(this.map, {
  45.         renderOptions: { map: this.map },
  46.         onSearchComplete: (results) => {
  47.           if (results.getNumPois()) {
  48.             const firstResult = results.getPoi(0);
  49.             const point = firstResult.point;
  50.             if (this.marker) {
  51.               this.map.removeOverlay(this.marker);
  52.             }
  53.             this.marker = new BMap.Marker(point);
  54.             this.map.addOverlay(this.marker);
  55.             this.map.centerAndZoom(point, 15);
  56.             this.updatePositionAndAddress(point);
  57.           }
  58.         }
  59.       });
  60.       local.search(this.keyMap);
  61.     }
  62.   },
  63.   mounted() {
  64.     if (!window.BMap) {
  65.       console.error('百度地图 API 未正确加载,请检查 API 密钥和脚本链接');
  66.       return;
  67.     }
  68.     this.map = new BMap.Map('map-container');
  69.     this.map.centerAndZoom(new BMap.Point(102.755, 25.134), 13);
  70.     this.map.enableScrollWheelZoom();
  71.     this.geoc = new BMap.Geocoder();
  72.     this.map.addEventListener('click', this.handleMapClick);
  73.   }
  74. };
  75. </script>
  76. <style scoped>
  77. .map-container {
  78.   width: 600px;
  79.   height: 500px;
  80. }
  81. .result {
  82.   margin-top: 10px;
  83.   font-size: 14px;
  84. }
  85. </style>
复制代码
1.4 v3组件


  1. <template>
  2.         <div>
  3.                 <!-- 地图容器 -->
  4.                 <div id="map-container" class="map-container"></div>
  5.                 <!-- 结果展示区域 -->
  6.                 <div class="result">
  7.                         搜索:<el-input v-model="keyMap" placeholder="请输入地址" size="small" clearable></el-input>
  8.                         <el-button type="primary" size="small" @click="searchAddress">搜索</el-button>
  9.                 </div>
  10.         </div>
  11. </template>
  12. <script setup lang="ts">
  13. import { onMounted, ref } from 'vue'
  14. const emit = defineEmits(['update:marker-position', 'update:address'])
  15. // 定义响应式数据
  16. const keyMap = ref('')
  17. let map: any = null
  18. let marker: any = null
  19. let geoc: any = null
  20. // 更新标记位置和地址的函数
  21. const updatePositionAndAddress = (point: any) => {
  22.         // 发送位置更新事件
  23.         emit('update:marker-position', {
  24.                 lat: point.lat,
  25.                 lng: point.lng
  26.         })
  27.         // 反向地理编码获取详细地址
  28.         geoc.getLocation(point, (rs: any) => {
  29.                 if (rs) {
  30.                         emit('update:address', rs.address)
  31.                 }
  32.         })
  33. }
  34. // 处理地图点击事件
  35. const handleMapClick = (e: any) => {
  36.         // 如果已有标注,则先移除
  37.         if (marker) {
  38.                 map.removeOverlay(marker)
  39.         }
  40.         // 创建新的标注
  41.         marker = new BMap.Marker(e.point)
  42.         map.addOverlay(marker)
  43.         // 更新位置和地址
  44.         updatePositionAndAddress(e.point)
  45. }
  46. // 搜索地址
  47. const searchAddress = () => {
  48.         if (!keyMap.value) return
  49.         // 创建地址搜索实例
  50.         const local = new BMap.LocalSearch(map, {
  51.                 renderOptions: { map: map },
  52.                 onSearchComplete: (results: any) => {
  53.                         if (results.getNumPois()) {
  54.                                 const firstResult = results.getPoi(0)
  55.                                 const point = firstResult.point
  56.                                
  57.                                 // 移除旧标记
  58.                                 if (marker) {
  59.                                         map.removeOverlay(marker)
  60.                                 }
  61.                                 // 添加新标记
  62.                                 marker = new BMap.Marker(point)
  63.                                 map.addOverlay(marker)
  64.                                 map.centerAndZoom(point, 15)
  65.                                 // 更新位置和地址
  66.                                 updatePositionAndAddress(point)
  67.                         }
  68.                 }
  69.         })
  70.         local.search(keyMap.value)
  71. }
  72. // 初始化地图
  73. onMounted(() => {
  74.         const BMap = window.BMap
  75.         if (!BMap) {
  76.                 console.error('百度地图 API 未正确加载,请检查 API 密钥和脚本链接')
  77.                 return
  78.         }
  79.         // 初始化地图
  80.         map = new BMap.Map('map-container')
  81.         map.centerAndZoom(new BMap.Point(102.755, 25.134), 13)
  82.         map.enableScrollWheelZoom()
  83.        
  84.         // 初始化地理编码器
  85.         geoc = new BMap.Geocoder()
  86.         // 添加点击事件监听
  87.         map.addEventListener('click', handleMapClick)
  88. })
  89. </script>
  90. <style scoped>
  91. /* 样式部分 */
  92. .map-container {
  93.         width:600px;
  94.         height: 500px;
  95. }
  96. .result {
  97.         margin-top: 10px;
  98.         font-size: 14px;
  99. }
  100. </style>
复制代码
1.5 利用 

  1. #需要完成源码加vx a2411286970a
  2.     <BaiduMap ref="mapRef"
  3. @update:marker-position="handleMarkerPositionUpdate"
  4. @update:address="handleAddressUpdate"></BaiduMap>
复制代码
  1. #需要完成源码加vx a2411286970a   
  2. handleMarkerPositionUpdate(position){
  3.      console.log(position);
  4.      this.ruleForm.latitude=position.lat
  5.      this.ruleForm.longitude=position.lng
  6.     },
复制代码
2.uinapp

我们在后台举行位置获取后,在前台微信小步伐及可进回显导航即可!
  1.                         // 导航功能
  2.                         onNavigateTap() {
  3.                                 // 检查是否有地址信息
  4.                                 if (!this.detail.jingdiandizhi) {
  5.                                         this.$utils.msg('暂无地址信息');
  6.                                         return;
  7.                                 }
  8.                                         let _this = this;
  9.                                 uni.authorize({
  10.                                         scope: 'scope.userLocation',
  11.                                         success: () => {
  12.                                                 // 用户已授权,获取当前位置
  13.                                                 uni.getLocation({
  14.                                                         type: 'gcj02', // 返回可以用于uni.openLocation的经纬度
  15.                                                         success: (res) => {
  16.                                                                 const latitude = res.latitude;
  17.                                                                 const longitude = res.longitude;
  18.                                                                 // 使用当前位置打开地图
  19.                                                                 uni.openLocation({
  20.                                                                         latitude: parseFloat(this.detail.latitude),
  21.                                                                         longitude:parseFloat(this.detail.longitude),
  22.                                                                         name: this.detail.jingdianmingcheng,
  23.                                                                         address: this.detail.jingdiandizhi,
  24.                                                                         scale: 18,
  25.                                                                         success: function() {
  26.                                                                                 console.log('成功打开位置');
  27.                                                                         },
  28.                                                                         fail: function(err) {
  29.                                                                                 console.error('打开位置失败', err);
  30.                                                                         }
  31.                                                                 });
  32.                                                         },
  33.                                                         fail: function(err) {
  34.                                                                 console.error('获取位置失败', err);
  35.                                                         }
  36.                                                 });
  37.                                         },
  38.                                         fail: () => {
  39.                                                 // 用户未授权
  40.                                                 uni.showModal({
  41.                                                         title: '提示',
  42.                                                         content: '需要获取您的位置权限,请前往设置开启。',
  43.                                                         success: (res) => {
  44.                                                                 if (res.confirm) {
  45.                                                                         uni.openSetting();
  46.                                                                 }
  47.                                                         }
  48.                                                 });
  49.                                         }
  50.                                 });
  51.                                
  52.                                 // // 打开地图导航
  53.                                 // uni.openLocation({
  54.                                 //         latitude: 0, // 这里需要替换为实际的经纬度
  55.                                 //         longitude: 0, // 这里需要替换为实际的经纬度
  56.                                 //         name: this.detail.jingdianmingcheng,
  57.                                 //         address: this.detail.jingdiandizhi,
  58.                                 //         success: function() {
  59.                                 //                 console.log('导航成功');
  60.                                 //         },
  61.                                 //         fail: function(err) {
  62.                                 //                 console.log('导航失败', err);
  63.                                 //                 // 如果没有经纬度,可以使用复制地址的方式
  64.                                 //                 uni.setClipboardData({
  65.                                 //                         data: this.detail.jingdiandizhi,
  66.                                 //                         success: function() {
  67.                                 //                                 uni.showToast({
  68.                                 //                                         title: '地址已复制,请打开地图APP进行导航',
  69.                                 //                                         icon: 'none'
  70.                                 //                                 });
  71.                                 //                         }
  72.                                 //                 });
  73.                                 //         }
  74.                                 // });
  75.                         },
复制代码

 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

八卦阵

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表