高德地图实现点击地图添加标记,通过输入框搜索详细地址自动定位到指定位置

高德地图api版本:JavaScript API V2.0.0

用高德地图实现输入搜索地址 添加标记,鼠标移动标记来获取当前该点的经纬度,详细地址

1. 高德地图的页面构建:首先先引入高德地图,增加自己想要的功能,我们这里需要一个输入联想框

2. 高德地图的Api来实现相关功能

  initMap() {
      const that = this
      return new Promise((reslove, reject) => {
        AMapLoader.load({
          key: 'cf5c437b14780406af75a81b380cafac',
          version: '2.0',
          plugins: [
            'AMap.ToolBar',
            'AMap.Scale',
            'AMap.Geocoder',
            'AMap.Geolocation',
            'AMap.PlaceSearch',
            'AMap.AutoComplete',
            'AMap.CitySearch'
          ],
          resizeEnable: true
        }).then((AMap) => {
          that.map = new AMap.Map('allmap', {
            resizeEnable: true,
            zoom: 14,
            viewMode: '3D', //使用3D视图
            center: [that.positionInfo.lng, that.positionInfo.lat]
          })
          that.getCurrentLocation()
          that.map.addControl(new AMap.Scale()) // 在图面添加比例尺控件,展示地图在当前层级和纬度下的比例尺
          that.map.addControl(new AMap.ToolBar()) //在图面添加鹰眼控件,在地图右下角显示地图的缩略图
          that.geocoder = new AMap.Geocoder({ radius: 1000, extensions: 'all', city: '全国' })
          that.mapSearchInit()
          that.geocoder.getAddress([that.positionInfo.lng, that.positionInfo.lat], function (status, result) {
            if (status === 'complete' && result.regeocode) {
              that.address = result.regeocode.formattedAddress
            } else {
              that.$message.error('根据经纬度查询地址失败')
            }
          })
        })
      })
    },

2. 根据输入框内容搜索地点,经纬度

 searchKeyWord() {
      let that = this
      that.placeSearchComponent.search(that.address, function (status, result) {
        if (status === 'complete' && result.info === 'OK') {
          that.show = true
          // 关键字联想的选项内容
          that.poiList = result.poiList.pois
        } else {
          that.showsearchResult = false
          that.poiList = []
          that.$message({
            message: '没有查到结果',
            type: 'warning'
          })
        }
      })
    },

3. 动态设置点标记,构造矢量圆形 

 dynamicSign(lng, lat, radius) {
      var marker = new AMap.Marker({
        position: new AMap.LngLat(lng, lat), //参数为经纬度
        draggable: true,
        cursor: 'move',
        riseOnHover: true,
        bubble: true,
        cursor: 'pointer'
      })
      // 构造矢量圆形
      const circle = new AMap.Circle({
        center: new AMap.LngLat(lng, lat), // 圆心位置
        radius: radius, //半径
        strokeColor: '#1890ff', //线颜色
        strokeOpacity: 1, //线透明度
        strokeWeight: 1, //线粗细度
        fillColor: '#1890ff', //填充颜色
        fillOpacity: 0.35 //填充透明度
      })
      this.map.clearMap()
      this.map.add([marker, circle]) // 添加点标志
      marker.on('dragend', this.markerClick)
    }

 高德地图api接口比较丰富,大多的需求都能实现。本次主要使用了POI搜索插件AMap.PlaceSearch,获取城市信息AMap.getCityInfo,设置标记点AMap.Marker,构造矢量图AMap.Circle结合了带列表的POI搜索的这个,再将监听事件改为列表点选的selectChange事件,获得当前点选地点经纬度,这样将二者进行了组合一下实现了以上的搜索以及展现方式。

  • 6
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用高德地图 JavaScript API 实现定位到当前位置可以分为以下几个步骤: 1. 引入高德地图 JavaScript API 的 SDK 文件 在 HTML 文件中引入高德地图 JavaScript API 的 SDK 文件,可以通过以下代码实现: ```html <script src="https://webapi.amap.com/maps?v=1.4.15&key=你的高德地图API密钥"></script> ``` 需要将 `key` 参数替换成自己的高德地图 API 密钥。 2. 创建地图实例 在 JavaScript 文件中创建地图实例,可以通过以下代码实现: ```javascript var map = new AMap.Map('container', { zoom: 16 // 地图缩放级别 }); ``` 其中 `container` 是地图容器的 ID,可以在 HTML 文件中创建一个 `div` 元素作为地图容器。 3. 获取当前位置 通过调用高德地图 JavaScript API 的 `AMap.Geolocation` 类实现获取当前位置,可以通过以下代码实现: ```javascript var geolocation = new AMap.Geolocation({ enableHighAccuracy: true, // 是否使用高精度定位,默认为 true timeout: 10000, // 超过 10 秒后停止定位,默认为无穷大 maximumAge: 0, // 定位结果缓存时间,默认为 0 convert: true, // 是否使用坐标转换服务,默认为 true showButton: false, // 是否显示定位按钮,默认为 true buttonPosition: 'LB', // 定位按钮的位置,默认为 'LB' buttonOffset: new AMap.Pixel(10, 10), // 定位按钮的偏移量,默认为 AMap.Pixel(10, 10) showMarker: false, // 是否显示定位,默认为 true markerOptions: {} // 定位的样式 }); geolocation.getCurrentPosition(function(status, result) { if (status === 'complete') { onComplete(result); } else { onError(result); } }); function onComplete(result) { var lng = result.position.getLng(); // 经度 var lat = result.position.getLat(); // 纬度 var accuracy = result.accuracy; // 精度 var address = result.formattedAddress; // 地址 console.log(lng, lat, accuracy, address); } function onError(result) { console.log(result.message); } ``` 在 `getCurrentPosition` 方法中传入两个回调函数,分别用于处理定位成功和失败的情况。在定位成功的回调函数中可以获取到当前位置的经纬度、精度和地址等信息。 4. 将地图定位到当前位置定位成功的回调函数中,可以将地图定位到当前位置,可以通过以下代码实现: ```javascript var marker = new AMap.Marker({ position: [lng, lat], // 定位位置 map: map // 地图实例 }); map.setCenter([lng, lat]); // 将地图中心设置为定位位置 ``` 在创建一个 `AMap.Marker` 实例作为定位,并将地图中心设置为定位位置实现地图定位到当前位置。 完整的实现代码如下: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>高德地图定位到当前位置</title> <style> #container { width: 100%; height: 500px; margin: 0 auto; } </style> </head> <body> <div id="container"></div> <script src="https://webapi.amap.com/maps?v=1.4.15&key=你的高德地图API密钥"></script> <script> var map = new AMap.Map('container', { zoom: 16 }); var geolocation = new AMap.Geolocation({ enableHighAccuracy: true, timeout: 10000, maximumAge: 0, convert: true, showButton: false, buttonPosition: 'LB', buttonOffset: new AMap.Pixel(10, 10), showMarker: false, markerOptions: {} }); geolocation.getCurrentPosition(function(status, result) { if (status === 'complete') { onComplete(result); } else { onError(result); } }); function onComplete(result) { var lng = result.position.getLng(); var lat = result.position.getLat(); var accuracy = result.accuracy; var address = result.formattedAddress; console.log(lng, lat, accuracy, address); var marker = new AMap.Marker({ position: [lng, lat], map: map }); map.setCenter([lng, lat]); } function onError(result) { console.log(result.message); } </script> </body> </html> ``` 在实现时需要将代码中的 `key` 参数替换成自己的高德地图 API 密钥。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值