mapbox实现点选要素

贴个群号

WebGIS学习交流群461555818,欢迎大家。

成果图

](https://img-blog.csdnimg.cn/direct/4e61853c0df547b8b5f333cd3077eb69.gif)

核心代码

核心逻辑就是指定一个唯一的高亮要素对象,全图监听点击事件,通过queryRenderedFeatures鼠标点拿到要素的id,然后将要素的状态改为选中,这样选中的要素的样式就会改为我们设置好的选中的样式,记住这个高亮对象,每次高亮的时候,将上一次的高亮要素的状态设置回未选中。

     //设置唯一高亮
     if (self.highLight.id !== null) {
       self.map.setFeatureState(
         { source: self.highLight.source, id: self.highLight.id },
         { select: false }
       );
     }
     self.highLight = {
       id:features[0].id,
       source:features[0].source
     };
     self.map.setFeatureState(
       { source: features[0].source, id: features[0].id },
       { select: true }
     );
//示例点线面样式
export const POINT_STYLE = {
    paint:{
        'circle-color': [
            'case',
            ['boolean', ['feature-state', 'select'], false],
            'rgb(0,255,255)',
            'orange'
        ],
    },
    layout: {
        visibility: 'visible',
    },
}
export const LINE_STYLE = {
    paint:{
        'line-color': [
            'case',
            ['boolean', ['feature-state', 'select'], false],
            'rgb(0,255,255)',
            '#ffffff'
        ],
        'line-width': [
            'case',
            ['boolean', ['feature-state', 'select'], false],
            8,
            2
        ],
    },
    layout: {
        visibility: 'visible',
    },
}
export const FILL_STYLE = {
    paint:{
        'fill-color': [
            'case',
            ['boolean', ['feature-state', 'select'], false],
            'rgb(0,255,255)',
             'orange'
        ],
        'fill-antialias': true,
        'fill-outline-color': "#ffffff",
        'fill-opacity': 0.5
    },
    layout: {
        visibility: 'visible',
    },
}
以下是一个简单的示例,演示如何在Mapbox实现框查圈查资源图层。这个示例使用的是Mapbox的JavaScript API和Turf.js库来处理地理数据。 ```javascript <!DOCTYPE html> <html> <head> <meta charset='utf-8' /> <title>Mapbox框查圈查资源图层示例</title> <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' /> <script src='https://api.mapbox.com/mapbox-gl-js/v2.4.0/mapbox-gl.js'></script> <link href='https://api.mapbox.com/mapbox-gl-js/v2.4.0/mapbox-gl.css' rel='stylesheet' /> <script src='https://api.mapbox.com/mapbox.js/plugins/turf/v3.0.11/turf.min.js'></script> <style> body { margin: 0; padding: 0; } #map { position: absolute; top: 0; bottom: 0; width: 100%; } .mapboxgl-popup { max-width: 400px; } </style> </head> <body> <div id='map'></div> <script> mapboxgl.accessToken = '<your access token>'; var map = new mapboxgl.Map({ container: 'map', style: 'mapbox://styles/mapbox/streets-v11', center: [-73.986, 40.743], zoom: 13 }); // 添加一个标记层 map.on('load', function() { map.addSource('markers', { type: 'geojson', data: { type: 'FeatureCollection', features: [ { type: 'Feature', geometry: { type: 'Point', coordinates: [-73.986, 40.743] }, properties: { name: '地点1' } }, { type: 'Feature', geometry: { type: 'Point', coordinates: [-73.984, 40.741] }, properties: { name: '地点2' } }, { type: 'Feature', geometry: { type: 'Point', coordinates: [-73.985, 40.744] }, properties: { name: '地点3' } } ] } }); map.addLayer({ id: 'markers', source: 'markers', type: 'circle', paint: { 'circle-radius': 8, 'circle-color': '#007cbf' } }); }); // 添加一个框查圈查控件 var draw = new MapboxDraw({ displayControlsDefault: false, controls: { rectangle: true, circle: true, point: false, line_string: false, polygon: false, trash: true } }); map.addControl(draw); // 框查圈查时触发 map.on('draw.selectionchange', function(e) { var features = map.queryRenderedFeatures(e.features[0]); // 计算被选中的要素的中心点 var centroid = turf.centroid(turf.featureCollection(features)); // 显示选中要素的信息 var popup = new mapboxgl.Popup() .setLngLat(centroid.geometry.coordinates) .setHTML('<h3>' + features.length + '个要素被选中</h3><p>' + features.map(function(feature) { return feature.properties.name; }).join(', ') + '</p>') .addTo(map); }); </script> </body> </html> ``` 这个示例添加了一个标记层,并给地图添加了一个框查圈查控件。当用户在地图上框选或圈选时,代码会查询被选中的要素,并计算它们的中心点。然后,代码会显示一个弹出窗口,其中包含被选中要素的信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值