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