[Mapbox GL]创建悬停效果

本文介绍如何使用Mapbox GL JS API中的queryRenderedFeatures和filter方法实现地图上特定区域的悬停样式变化。通过添加多个图层并利用filter进行数据筛选,实现了当鼠标悬停在某个行政区划上时,该区域的颜色会发生变化的效果。
摘要由CSDN通过智能技术生成

         使用queryRenderedFeatures和filter来改变悬停样式


<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8' />
    <title></title>
    <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
    <script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.29.0/mapbox-gl.js'></script>
    <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.29.0/mapbox-gl.css' rel='stylesheet' />
    <style>
        body { margin:0; padding:0; }
        #map { position:absolute; top:0; bottom:0; width:100%; }
    </style>
</head>
<body>

<div id='map'></div>
<script>
mapboxgl.accessToken = '<your access token here>';
var map = new mapboxgl.Map({
    container: 'map',
    style: 'mapbox://styles/mapbox/streets-v9',
    center: [-100.486052, 37.830348],
    zoom: 2
});


map.on('load', function () {
    map.addSource("states", {
        "type": "geojson",   /* geojson类型source */
        "data": "https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_110m_admin_1_states_provinces.geojson"
    });

    map.addLayer({
        "id": "state-fills",
        "type": "fill",       /* fill类型layer一般用来表示面 */
        "source": "states",
        "layout": {},
        "paint": {
            "fill-color": "#627BC1", /* layer颜色 */
            "fill-opacity": 0.5      /* layer透明度 */
        }
    });

    map.addLayer({
        "id": "state-borders",
        "type": "line",      /* line类型layer表示线 */
        "source": "states",
        "layout": {},
        "paint": {
            "line-color": "#627BC1", /* 线条颜色 */
            "line-width": 2          /* 线条宽度 */
        }
    });

    map.addLayer({
        "id": "state-fills-hover",
        "type": "fill",
        "source": "states",
        "layout": {},
        "paint": {
            "fill-color": "#627BC1",
            "fill-opacity": 1
        },
        "filter": ["==", "name", ""]  /* 过滤器,名字为空的数据才显示,也就是默认不使用该layer  */
    });

    // When the user moves their mouse over the page, we look for features
    // at the mouse position (e.point) and within the states layer (states-fill).
    // If a feature is found, then we'll update the filter in the state-fills-hover
    // layer to only show that state, thus making a hover effect.
    map.on("mousemove", function(e) {
        var features = map.queryRenderedFeatures(e.point, { layers: ["state-fills"] });/*queryRenderedFeatures  ([geometry], [parameters]):返回满足查询条件并且能够可视化的Geojson特性对象数组,查询条件可以是layers或者filter,如果是layers,则在这些layer之内的特性能够返回  */
        if (features.length) {
            map.setFilter("state-fills-hover", ["==", "name", features[0].properties.name]); /* 通过设置filter更新要显示的数据,即出现鼠标悬停之后的变色效果 */
        } else {
            map.setFilter("state-fills-hover", ["==", "name", ""]);
        }
    });

    // Reset the state-fills-hover layer's filter when the mouse leaves the map
    map.on("mouseout", function() {
        map.setFilter("state-fills-hover", ["==", "name", ""]); /* 鼠标移开时还原layer的过滤器 */
    });
});
</script>

</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值