高德实现简单的地图 +定位@amap/amap-jsapi-loader 经纬度

<template>
        <div class="map_box">
          <div id="map"></div>
        </div>

</template>
<script>
import AMapLoader from '@amap/amap-jsapi-loader'
export default {
  data() {
    return {
    }
  },

  methods:{
    
    initAMap(jing, wei) {
      let _this = this
      AMapLoader.load({
        key: 'ccbad877e365ac35426acc236edf88b3',
        version: '2.0',
        plugins: [
          'AMap.Scale',
          'AMap.ToolBar',
          'AMap.ControlBar',
          'AMap.Geocoder',
          'AMap.Marker',
          'AMap.CitySearch',
          'AMap.Geolocation',
          'AMap.AutoComplete',
          'AMap.InfoWindow',
          'AMap.PlaceSearch'
        ]
      }).then(AMap => {
        var address = '浙江省杭州市拱墅区东方茂商业中心2幢1715-1716室'
        var autoOptions = {
          city: '杭州市',

          map: _this.map, // 展现结果的地图实例

          pageSize: 1, // 单页显示结果条数

          pageIndex: 1, // 页码

          autoFitView: true // 是否自动调整地图视野使绘制的 Marker点都处于视口的可见范围
        }
        var placeSearch = new AMap.PlaceSearch(autoOptions)
        placeSearch.search(address, function(status, result) {
          // 搜索成功时,result即是对应的匹配数据
          if (status == 'complete') {
            if (result.poiList.pois.length == 0) {
              // console.log('没有查询到对应的地址')
            } else {
              let lng = result.poiList.pois[0].location.lng

              let lat = result.poiList.pois[0].location.lat

              _this.map = new AMap.Map('map', {
                zoom: 13, //初始化地图级别
                center: [lng, lat] // 初始化中心点坐标 广州
              })

              const marker = new AMap.Marker({
                position: [lng, lat] //位置
              })
              _this.map.add(marker) //添加到地图
            }
          } else if (status == 'no_data') {
            // console.log('没有查询到对应的地址')
          }
        })
      })
    },
  },
  mounted(){
    this.initAMap(116.39, 39.9)
  },

}
</script>
<style lang="less" scoped>
.map_box {
                width: 97%;
                height: 400px;
                border: 1px solid #333;
                margin: 10px auto;
            
                #map {
                  width: 100%;
                  height: 100%;
                }
              }
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用`@amap/amap-jsapi-loader`库来加载高德地图JSAPI,并在Vue组件中使用。以下是一个示例代码: 首先,安装依赖: ```bash npm install @amap/amap-jsapi-loader ``` 然后,在Vue组件中使用: ```vue <template> <div ref="map" style="height: 400px;"></div> </template> <script> import AMapLoader from '@amap/amap-jsapi-loader'; export default { name: 'AmapDemo', data() { return { map: null, marker: null, }; }, mounted() { AMapLoader.load({ key: 'your_amap_key', version: '2.0', plugins: ['AMap.Geocoder'], }).then((AMap) => { const map = new AMap.Map(this.$refs.map, { zoom: 13, center: [116.39, 39.9], }); map.on('click', (e) => { const geocoder = new AMap.Geocoder({ city: '全国', }); geocoder.getAddress([e.lnglat.getLng(), e.lnglat.getLat()]).then((result) => { const address = result.regeocode.formattedAddress; const position = [e.lnglat.getLng(), e.lnglat.getLat()]; if (this.marker) { this.marker.setPosition(position); } else { this.marker = new AMap.Marker({ position, map, }); } console.log('Address:', address); console.log('Position:', position); }); }); this.map = map; }); }, }; </script> ``` 在上面的代码中,我们首先使用`AMapLoader.load`方法加载高德地图JSAPI,并在`then`方法中初始化地图。 然后,我们在地图上添加一个`click`事件,当用户点击地图时,我们使用`AMap.Geocoder`插件获取该位置的地址和经纬度,并将其打印到控制台中。如果已经存在标记,则更新标记的位置,否则创建一个新的标记。 注意,在上面的示例代码中,我们需要将`your_amap_key`替换为你自己的高德地图开发者Key。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值