MapBox實(shí)現(xiàn)自定義圖標(biāo)热鞍,并且根據(jù)數(shù)據(jù)定義圖標(biāo)大小和顏色葫慎,以及鼠標(biāo)經(jīng)過顯示信息框

Mapbox GL JS

MapboxGLJS是一個(gè)JavaScript庫(kù),它使用WebGL呈現(xiàn)交互式地圖vector tiles 和Mapbox styles....它是Mapbox GL生態(tài)系統(tǒng)的一部分薇宠,包括 Mapbox Mobile 偷办。

此文為一個(gè)小例子,需求是要根據(jù)數(shù)據(jù)來在地圖上畫不同顏色和不同大小的圓圈澄港,還要在鼠標(biāo)經(jīng)過這些圓圈時(shí)椒涯,彈出一個(gè)信息框,展示一些重要信息回梧,關(guān)鍵代碼如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8' />
    <title>Display a popup on hover</title>
    <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
    <script src='https://api.tiles.mapbox.com/mapbox-gl-js/v1.5.0/mapbox-gl.js'></script>
    <link  rel='stylesheet' />
    <style>
        body { margin:0; padding:0; }
        #map { position:absolute; top:0; bottom:0; width:100%; }
        .my-icon {
            border-radius: 100%;
            width: 20px;
            height: 20px;
            text-align: center;
            line-height: 20px;
            color: white;
        }

        .icon-dc {
            background: #3bb2d0;
        }
    </style>
</head>
<body>

<style>
    .mapboxgl-popup {
        max-width: 400px;
        font: 12px/20px 'Helvetica Neue', Arial, Helvetica, sans-serif;
    }
</style>
<div id='map'></div>
<script>
mapboxgl.accessToken = '填寫你自己的';

var map = new mapboxgl.Map({
    container: 'map',
    style: 'mapbox://styles/mapbox/light-v9',
    center: [-77.04, 38.907],
    zoom: 11.15
});

map.on('load', function() {

    // Add a layer showing the places.
    map.addLayer({
        "id": "places",
        "type": "circle",
        "source": {
            "type": "geojson",
            "data": {
                "type": "FeatureCollection",
                "features": [{
                    "type": "Feature",
                    "properties": {
                        "description": "<strong>路口信息</strong><br>" +
                            "<span class='pull-left'>編號(hào):11111111111111111111111111111-</span><span class='pull-right'>名稱:-</span><br>" ,
                        "avg": 10
                        //"icon": "theatre"    
                    },
                    "geometry": {
                        "type": "Point",
                        "coordinates": [-77.038659, 38.931567]
                    }
                }, {
                    "type": "Feature",
                    "properties": {
                        "description": "<strong>Mad Men Season Five Finale Watch Party</strong><p>Head to Lounge 201 (201 Massachusetts Avenue NE) Sunday for a Mad Men Season Five Finale Watch Party, complete with 60s costume contest, Mad Men trivia, and retro food and drink. 8:00-11:00 p.m. $10 general admission, $20 admission and two hour open bar.</p>",
                        "avg": 20
                        //"icon": "theatre"
                    },
                    "geometry": {
                        "type": "Point",
                        "coordinates": [-77.003168, 38.894651]
                    }
                }, {
                    "type": "Feature",
                    "properties": {
                        "description": "<strong>Big Backyard Beach Bash and Wine Fest</strong><p>EatBar (2761 Washington Boulevard Arlington VA) is throwing a Big Backyard Beach Bash and Wine Fest on Saturday, serving up conch fritters, fish tacos and crab sliders, and Red Apron hot dogs. 12:00-3:00 p.m. $25.grill hot dogs.</p>",
                        "avg": 30
                        //"icon": "bar"
                    },
                    "geometry": {
                        "type": "Point",
                        "coordinates": [-77.090372, 38.881189]
                    }
                }, {
                    "type": "Feature",
                    "properties": {
                        "description": "<strong>Ballston Arts & Crafts Market</strong><p>The Ballston Arts & Crafts Market sets up shop next to the Ballston metro this Saturday for the first of five dates this summer. Nearly 35 artists and crafters will be on hand selling their wares. 10:00-4:00 p.m.</p>",
                        "avg": 40
                        //"icon": "art-gallery"
                    },
                    "geometry": {
                        "type": "Point",
                        "coordinates": [-77.111561, 38.882342]
                    }
                }, {
                    "type": "Feature",
                    "properties": {
                        "description": "<strong>Seersucker Bike Ride and Social</strong><p>Feeling dandy? Get fancy, grab your bike, and take part in this year's Seersucker Social bike ride from Dandies and Quaintrelles. After the ride enjoy a lawn party at Hillwood with jazz, cocktails, paper hat-making, and more. 11:00-7:00 p.m.</p>",
                        "avg": 50
                        //"icon": "bicycle"
                    },
                    "geometry": {
                        "type": "Point",
                        "coordinates": [-77.052477, 38.943951]
                    }
                }, {
                    "type": "Feature",
                    "properties": {
                        "description": "<strong>Capital Pride Parade</strong><p>The annual Capital Pride Parade makes its way through Dupont this Saturday. 4:30 p.m. Free.</p>",
                        "avg": 60
                        //"icon": "star"
                    },
                    "geometry": {
                        "type": "Point",
                        "coordinates": [-77.043444, 38.909664]
                    }
                }, {
                    "type": "Feature",
                    "properties": {
                        "description": "<strong>Muhsinah</strong><p>Jazz-influenced hip hop artist Muhsinah plays the Black Cat (1811 14th Street NW) tonight with Exit Clov and Gods’illa. 9:00 p.m. $12.</p>",
                        "avg": 70
                        //"icon": "music"
                    },
                    "geometry": {
                        "type": "Point",
                        "coordinates": [-77.031706, 38.914581]
                    }
                }, {
                    "type": "Feature",
                    "properties": {
                        "description": "<strong>A Little Night Music</strong><p>The Arlington Players' production of Stephen Sondheim's <em>A Little Night Music</em> comes to the Kogod Cradle at The Mead Center for American Theater (1101 6th Street SW) this weekend and next. 8:00 p.m.</p>",
                        "avg": 1
                        //"icon": "music"
                    },
                    "geometry": {
                        "type": "Point",
                        "coordinates": [-77.020945, 38.878241]
                    }
                }, {
                    "type": "Feature",
                    "properties": {
                        "description": "<strong>Truckeroo</strong><p>Truckeroo brings dozens of food trucks, live music, and games to half and M Street SE (across from Navy Yard Metro Station) today from 11:00 a.m. to 11:00 p.m.</p>",
                        "avg": 16
                        //"icon": "music"
                    },
                    "geometry": {
                        "type": "Point",
                        "coordinates": [-77.007481, 38.876516]
                    }
                }]
            }
        },
        "paint": {
            "circle-radius": [
                            "step",
                            ["get", "avg"],
                            10,
                            6,
                            15,
                            10,
                            20
                        ],
            "circle-color": [
                            "step",
                            ["get", "avg"],
                            "#51bbd6",
                            10,
                            "#f1f075",
                            40,
                            "#f28cb1"
                        ]

        }, 
        /*"layout": {
            "icon-image": "{icon}-15",
            "icon-allow-overlap": true
        }*/
    });

    // Create a popup, but don't add it to the map yet.
    var popup = new mapboxgl.Popup({
        closeButton: true,
        closeOnClick: false
    });

    map.on('mouseenter', 'places', function(e) {
        // Change the cursor style as a UI indicator.
        map.getCanvas().style.cursor = 'pointer';

        var coordinates = e.features[0].geometry.coordinates.slice();
        var description = e.features[0].properties.description;

        // Ensure that if the map is zoomed out such that multiple
        // copies of the feature are visible, the popup appears
        // over the copy being pointed to.
        while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
            coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
        }

        // Populate the popup and set its coordinates
        // based on the feature found.
        popup.setLngLat(coordinates)
            .setHTML(description)
            .addTo(map);
    });

    /*map.on('mouseleave', 'places', function() {
        map.getCanvas().style.cursor = '';
        popup.remove();
    });*/
});
</script>

</body>
</html>
image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末废岂,一起剝皮案震驚了整個(gè)濱河市祖搓,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌湖苞,老刑警劉巖拯欧,帶你破解...
    沈念sama閱讀 217,185評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異袒啼,居然都是意外死亡哈扮,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門蚓再,熙熙樓的掌柜王于貴愁眉苦臉地迎上來滑肉,“玉大人,你說我怎么就攤上這事摘仅“忻恚” “怎么了?”我有些...
    開封第一講書人閱讀 163,524評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵娃属,是天一觀的道長(zhǎng)六荒。 經(jīng)常有香客問我,道長(zhǎng)矾端,這世上最難降的妖魔是什么掏击? 我笑而不...
    開封第一講書人閱讀 58,339評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮秩铆,結(jié)果婚禮上砚亭,老公的妹妹穿的比我還像新娘。我一直安慰自己殴玛,他們只是感情好捅膘,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,387評(píng)論 6 391
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著滚粟,像睡著了一般寻仗。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上凡壤,一...
    開封第一講書人閱讀 51,287評(píng)論 1 301
  • 那天署尤,我揣著相機(jī)與錄音,去河邊找鬼亚侠。 笑死曹体,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的盖奈。 我是一名探鬼主播混坞,決...
    沈念sama閱讀 40,130評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼狐援,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼钢坦!你這毒婦竟也來了究孕?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,985評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤爹凹,失蹤者是張志新(化名)和其女友劉穎厨诸,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體禾酱,經(jīng)...
    沈念sama閱讀 45,420評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡微酬,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,617評(píng)論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了颤陶。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片颗管。...
    茶點(diǎn)故事閱讀 39,779評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖滓走,靈堂內(nèi)的尸體忽然破棺而出垦江,到底是詐尸還是另有隱情,我是刑警寧澤搅方,帶...
    沈念sama閱讀 35,477評(píng)論 5 345
  • 正文 年R本政府宣布比吭,位于F島的核電站,受9級(jí)特大地震影響姨涡,放射性物質(zhì)發(fā)生泄漏衩藤。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,088評(píng)論 3 328
  • 文/蒙蒙 一涛漂、第九天 我趴在偏房一處隱蔽的房頂上張望赏表。 院中可真熱鬧,春花似錦怖喻、人聲如沸底哗。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)跋选。三九已至,卻和暖如春哗蜈,著一層夾襖步出監(jiān)牢的瞬間前标,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工距潘, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留炼列,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,876評(píng)論 2 370
  • 正文 我出身青樓音比,卻偏偏與公主長(zhǎng)得像俭尖,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,700評(píng)論 2 354