Vue+Tencent location service realizes coordinate picker function

demand

1. Search for a specific address, automatically fill in the latitude and longitude, and mark it on the map

2. Click on a point on the map to refill the latitude and longitude and mark

Code

  • Create a new div in dom to render the map
<el-form-item label="店铺地址" prop="address">
  <el-input v-model="fristForm.address"></el-input>
  <el-input
    class="long-lat"
    v-model="fristForm.longitude"
    placeholder="经度"
  ></el-input>
  <el-input
    class="long-lat"
    v-model="fristForm.latitude"
    placeholder="纬度"
  ></el-input>
  <el-button size="mini" type="primary" @click="searchKeyword"
    >搜索</el-button
  >
</el-form-item>
<span class="changeAddress">点击地图更换分店定位地址</span>
<!-- 渲染地图的div容器 -->
<div id="container" class="mapbox"></div>
  • js defines map variables and sets requirements
var searchService,geocoder,map,markersArray = [];
<script>
export default {
    mounted() {
      this.init();
    },
    methods:{
        init() {
          var that = this;
          var center = new qq.maps.LatLng(39.916527, 116.397128);
          var map = new qq.maps.Map(document.getElementById("container"), {
            center: center,
            zoom: 13
          });
          var latlngBounds = new qq.maps.LatLngBounds();
          qq.maps.event.addListener(map, "click", function(event) {
            console.log(event);
            that.fristForm.longitude = event.latLng.getLng(); // 经度
            that.fristForm.latitude = event.latLng.getLat(); // 纬度

            if (markersArray) {
              for (let i in markersArray) {
                markersArray[i].setMap(null);
              }
            }
            var marker = new qq.maps.Marker({
              map: map,
              position: event.latLng
            });
            markersArray.push(marker);
          });

          geocoder = new qq.maps.Geocoder({
            complete: function(result) {
              console.log(result);
              that.fristForm.longitude = result.detail.location.lng;
              that.fristForm.latitude = result.detail.location.lat;
              map.setCenter(result.detail.location);
              var marker = new qq.maps.Marker({
                map: map,
                position: result.detail.location
              });
              markersArray.push(marker);
            }
          });
        },
    },
    // 搜索地址
    searchKeyword() {
      var keyword = this.fristForm.address;
      this.clearOverlays(markersArray);
      //根据输入的城市设置搜索范围
      // searchService.setLocation("北京");
      //根据输入的关键字在搜索范围内检索
      if (keyword) {
        // searchService.search(keyword);
        geocoder.getLocation(keyword);
      } else {
        alert("请输入地址");
      }
    },
}
</script>

Document reference

The above code uses the jsapi function, and the corresponding function has been upgraded to JavaScript API GL. The address resolution function can directly call the interface for use. Welcome everyone to experience!

Address resolution (address to coordinates)

JavaScript API GL Reference Manual

Author: houqq

Link: https://segmentfault.com/a/1190000022211912

Source: segmentfault

The copyright belongs to the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

Guess you like

Origin blog.csdn.net/weixin_45628602/article/details/109671805