mapbox-gl 根据区域线json放置文本text,鼠标移入

mapboxgl,turf安装

npm install turf
npm install --save mapbox-gl

mapboxgl,turf引入

import turf from 'turf/turf'
import 'mapbox-gl/dist/mapbox-gl.css'
import mapboxgl from 'mapbox-gl';

townData格式

var townData = {
  type:'FeatureCollection',
  features:[
  {
    'type': 'Polygon',
      'coordinates':[[[117.26,27.34],[117.26,27.38]...]]
  },
  {
    'type': 'Polygon',
      'coordinates':[[[117.26,27.34],[117.26,27.38]...]]
  },
  {
    'type': 'Polygon',
      'coordinates':[[[117.26,27.34],[117.26,27.38]...]]
  }....
  ]
}

地图创建

<div id="map"></div>

this.mapbox = new mapboxgl.Map({
        container: 'map',
        style: {
          "version": 8,
          "glyphs": "./static/lib/mapbox-gl-js/fonts/{fontstack}/{range}.pbf",
          "sources": {
            "geoq.tile": {
              "type": "raster",
              "tiles": [
                `${process.env.VUE_APP_MAPURL}/arcgis/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}`
              ],
              "tileSize": 256
            }
          },
          "layers": [{
            "id": "geoq",
            "type": "raster",
            "source": "geoq.tile"
          }]
        },
        center: [this.longitude,this.latitude],
        zoom: this.zoom,
        pitch: 30,
        bearing: 0
      });
this.mapbox.on('load', ()=>{
  this.addLabelLayer('town-label', this.townData);
})

添加label方法

addLabelLayer(id, data){
      var newData = {
        type:'FeatureCollection',
        features:[]
      }
      data.features.forEach(feature=>{
        var centroidPt = turf.centroid(feature)
        centroidPt.properties = feature.properties
        newData.features.push(centroidPt)
      })
      if(this.mapbox.getLayer(id)){
        this.mapbox.removeLayer(id);
      }
      this.mapbox.addSource(id, {
        'type': 'geojson',
        'data': newData
      });
      this.mapbox.addLayer({
        id: id,
        type: "symbol",
        source: id,
        layout: {
          "text-field": ["get", "NAME"],
          "text-variable-anchor": ["top", "bottom", "left", "right"],
          "text-radial-offset": 0.5,
          "text-justify": "auto",
          // "icon-image": image
        },
        paint: {
          'text-color': '#ff0'
        }
      });
    },

添加鼠标移入移出弹窗,注:'symbols'为icon图标id和source

var popup = new mapboxgl.Popup({
          closeButton: false,
          closeOnClick: false
        });
        this.mapbox.on('mouseenter', 'symbols', (e)=> {
          debugger
          const features = this.mapbox.queryRenderedFeatures(e.point);
          if(features.length){
            this.mapbox.getCanvas().style.cursor = 'pointer';
            var coordinates = e.features[0].geometry.coordinates.slice()
            var popupName = e.features[0].properties.name
            var popupvalue = e.features[0].properties.value
            const contain = `
              <div class="popupClass">
                <div class="popupClass-title">
                  <span>名称:</span>
                  <span>${popupName}</span>
                </div>
                <div class="popupClass-title">
                  <span>值:</span>
                  <span>${popupvalue}</span></div>
              </div>
            `
            popup.setLngLat(coordinates)
            .setHTML(contain)
            .addTo(this.mapbox);
          }
        })
        this.mapbox.on('mouseleave', 'symbols', (e)=> {
          this.mapbox.getCanvas().style.cursor = '';
          popup.remove(); 
        })

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

我就是你的语法糖️

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

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

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

打赏作者

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

抵扣说明:

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

余额充值