vue3使用高德地图获取经纬度

首先 安装依赖

cnpm i @amap/amap-jsapi-loader --save

<script setup>
import { onMounted, reactive, ref } from 'vue'
import AMapLoader from '@amap/amap-jsapi-loader'

const map = ref(null)
const mapData = reactive({
  map: {},
  keyword: '',
  selectedLocation: {},
  selectedAddress: '',
  marker: null, // 用于存储选中位置的标记
})
const lng = ref(0)
const lat = ref(0)
onMounted(() => {
  window._AMapSecurityConfig = {
    securityJsCode: '', // 密钥
  }

  AMapLoader.load({
    key: '', // key值
    version: '1.4.15',
    plugins: ['AMap.PlaceSearch'],
  })
    .then((AMap) => {
      const mapInstance = new AMap.Map('container', {
        viewMode: '2D',
        zoom: 11,
        center: [116.397033, 39.917606],
        layers: [
          new AMap.TileLayer.RoadNet(),
          new AMap.TileLayer.WMS({
            url: 'https://ahocevar.com/geoserver/wms',
            blend: false,
            tileSize: 512,
            params: {
              LAYERS: 'topp:states',
              VERSION: '1.3.0',
            },
          }),
        ],
        scrollWheel: true, // 启用滚动缩放
      })

      mapInstance.on('click', (e) => {
        lng.value = e.lnglat.getLng()
        lat.value = e.lnglat.getLat()

        // 移除之前的标记
        if (mapData.marker) {
          mapData.marker.setMap(null)
        }

        // 创建新的标记
        const marker = new AMap.Marker({
          position: [lng.value, lat.value],
          icon: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png', // 选中位置的图标
          offset: new AMap.Pixel(-15, -30), // 调整图标的偏移位置
        })

        // 将标记添加到地图
        marker.setMap(mapInstance)

        // 存储选中位置的标记
        mapData.marker = marker

        // 将地图中心设置为选中位置
        mapData.map.setCenter([lng.value, lat.value])
      })

      mapData.map = mapInstance
    })
    .catch((e) => {
      console.log(e)
    })
})

function search() {
  if (mapData.keyword) {
    AMapLoader.load({
      key: '',
      version: '1.4.15',
      plugins: ['AMap.PlaceSearch'],
    })
      .then((AMap) => {
        const placeSearch = new AMap.PlaceSearch({
          city: '全国', // 请根据实际情况设置城市
          map: mapData.map,
        })
        placeSearch.search(mapData.keyword, (status, result) => {
          console.log(result) // 输出完整的搜索结果

          if (status === 'complete') {
            const pois = result.poiList.pois
            if (pois.length > 0) {
              const { location } = pois[0]

              // 移除之前的标记
              if (mapData.marker) {
                mapData.marker.setMap(null)
              }

              // 创建新的标记
              const marker = new AMap.Marker({
                position: location,
                icon: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png',
                offset: new AMap.Pixel(-15, -30),
              })

              // 将标记添加到地图
              marker.setMap(mapData.map)

              // 存储选中位置的标记
              mapData.marker = marker

              // 将地图中心设置为选中位置
              mapData.map.setCenter(location)
            }
          }
          else {
            console.log('搜索失败或无结果')
          }
        })
      })
      .catch((e) => {
        console.log(e)
      })
  }
}

function zoomIn() {
  mapData.map.zoomIn()
}

function zoomOut() {
  mapData.map.zoomOut()
}
</script>

<template>
  <div>
    <input v-model="mapData.keyword" style="width: 80%; height: 30px;" @keydown.enter="search">
    <button @click="search">
      搜索
    </button>
    <button @click="zoomIn">
      放大
    </button>
    <button @click="zoomOut">
      缩小
    </button>
    <div id="container" class="map" style="height: 400px; border-radius: 5px;" />
    <div>经度:{{ lng }}</div>
    <div>经度:{{ lat }}</div>
  </div>
</template>

<style scoped>
  .map {
    width: 100%;
  }
</style>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
你可以使用 Vue高德地图的 JavaScript API 来获取经纬度信息。下面是一个简单的示例: 1. 首先,在你的 Vue 项目中安装高德地图的 JavaScript API。你可以通过在 HTML 文件的 `<head>` 标签中添加以下代码来引入高德地图的 API: ```html <script src="https://webapi.amap.com/maps?v=1.4.15&key=YOUR_API_KEY"></script> ``` 将 `YOUR_API_KEY` 替换为你自己的高德地图 API 密钥。 2. 在你需要获取经纬度信息的组件中,添加一个地图容器和一个按钮: ```html <template> <div> <div id="mapContainer"></div> <button @click="getCoordinates">获取经纬度</button> </div> </template> ``` 3. 在组件的 `<script>` 标签中,添加获取经纬度的方法: ```javascript <script> export default { methods: { getCoordinates() { // 创建地图实例 const map = new AMap.Map('mapContainer', { zoom: 16, // 设置地图缩放级别 center: [116.397428, 39.90923], // 设置地图中心点坐标 }); // 添加点击事件监听器 map.on('click', (e) => { const { lng, lat } = e.lnglat; // 获取点击位置的经纬度 // 在这里可以进行经纬度的后续处理,比如发送到服务器等 console.log(`经度:${lng},纬度:${lat}`); }); }, }, }; </script> ``` 通过以上代码,当用户点击 "获取经纬度" 的按钮时,会在控制台输出经纬度信息。 注意:以上代码只是一个简单示例,你可以根据自己的需求进行进一步的开发和优化。同时,如果你需要更多高德地图 API 的功能和详细参数配置,请参考高德地图开发文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

淡忘_cx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值