web端使用腾讯地图进行路线配置

实现效果

地图容器:

<div v-loading="loading" id="map" class=" height-percent-100" style="overflow:hidden;cursor: grab"></div>

创建地图对象:

    /**
     * 初始化地图
     */
    initMap () {
      const loading = this.$loading({
        lock: true,
        text: '初始化地图中',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      // 体谅一下网慢的,等在线路程js加载完
      setTimeout(() => {
        try {
          // eslint-disable-next-line no-undef
          this.map = new TMap.Map('map', {
            // eslint-disable-next-line no-undef
            center: new TMap.LatLng(this.center.lat, this.center.lng), // 地图显示中心点
            zoom: 12// 缩放级别
          });
          if (this.mapData) {
            this.routes = this.mapData?.routes.length ? this.mapData.routes.map(item => {
              return {
                ...item,
                title: item.roadName,
                policy: item.policy || 'REAL_TRAFFIC'
              }
            }) : []
            this.roadKm = this.mapData?.roadKm || 0
            if (this.routes.length) {
              this.start()
            }
          }
          this.$nextTick(() => {
            loading.close()
          })
        } catch (err) {
          this.$message.warning('地图初始化失败,请检查网络')
          loading.close()
        }
      }, 500)
    },

 选择路径后规划路线:

    /**
     * 开始路径规划
     */
    async start () {
      this.loading = true
      let distance = 0
      const coords = []
      for (const [index] of this.routes.entries()) {
        if (index) {
          await this.request(index).then(res => {
            distance += res.distance
            coords.push(...res.polyline)
          }).catch(() => {
            this.loading = false
          })
        }
      }
      this.roadKm = Number((distance / 1000).toFixed(2))
      const pl = [];
      // 坐标解压(返回的点串坐标,通过前向差分进行压缩)
      const kr = 1000000;
      for (let i = 2; i < coords.length; i++) {
        coords[i] = Number(coords[i - 2]) + Number(coords[i]) / kr;
      }
      // 将解压后的坐标放入点串数组pl中
      for (let i = 0; i < coords.length; i += 2) {
        // eslint-disable-next-line no-undef
        pl.push(new TMap.LatLng(coords[i], coords[i + 1]));
      }
      if (this.polylineLayer) {
        this.polylineLayer.setMap(null)
        this.polylineLayer = null
      }
      this.display_polyline(pl)
      if (this.marker) {
        this.marker.setMap(null)
        this.marker = null
      }
      this.display_marker(this.routes)
      this.loading = false
    },

好的,您可以使用腾讯地图提供的 JavaScript API,在您的网站中嵌入地图,并且在地图上添加地址选择功能。以下是实现步骤: 1. 在腾讯地图开放平台申请密钥。 2. 在您网站的 HTML 页面中引入 JavaScript API: ```html <script src="https://map.qq.com/api/js?v=2.exp&key=YOUR_KEY"></script> ``` 3. 创建地图容器: ```html <div id="map" style="width: 100%; height: 500px;"></div> ``` 4. 初始化地图: ```javascript var map = new qq.maps.Map(document.getElementById("map"), { center: new qq.maps.LatLng(39.916527, 116.397128), // 地图中心点 zoom: 13, // 地图缩放级别 }); ``` 5. 添加地址选择控件: ```javascript var geocoder = new qq.maps.Geocoder({ complete: function (result) { // 地址解析完成后的回调函数 console.log(result.detail.addressComponents); }, }); var searchService = new qq.maps.SearchService({ complete: function (results) { // 搜索完成后的回调函数 var poi = results.detail.pois[0]; map.setCenter(poi.latLng); marker.setPosition(poi.latLng); geocoder.getAddress(poi.latLng); }, }); var marker = new qq.maps.Marker({ position: map.getCenter(), map: map, }); var searchBox = new qq.maps.SearchBox(document.getElementById("searchBox"), { location: map.getCenter(), pageCapacity: 5, draggable: true, onConfirm: function (poi) { // 确认选择地址后的回调函数 map.setCenter(poi.latLng); marker.setPosition(poi.latLng); geocoder.getAddress(poi.latLng); }, onMarkerDrag: function (position) { // 拖拽标记后的回调函数 geocoder.getAddress(position); }, }); searchBox.search(""); ``` 以上代码实现了一个简单的地图地址选择功能,您可以根据实际需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值