2 Star 0 Fork 2

n7gs7/leaflet-book

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
geojson.html 3.46 KB
一键复制 编辑 原始数据 按行查看 历史
fengfeng043 提交于 2019-10-09 08:43 . 使用实际含义修改文件命名
<!DOCTYPE html>
<html>
<head>
<title>Leaflet1</title>
<meta charset="utf-8">
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.5.1/dist/leaflet.css" integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ==" crossorigin=""/>
<script src="https://unpkg.com/leaflet@1.5.1/dist/leaflet.js" integrity="sha512-GffPMF3RvMeYyc1LWMHtK8EbPv0iNZ8/oTtHPx9/cc2ILxQ+u905qIwdpULaqDkyBKgOaB57QTMg7ztg8Jm2Og==" crossorigin=""></script>
<!--<script src="https://cdnjs.com/libraries/leaflet-tilelayer-geojson"></script>-->
<style TYPE="text/css">
body {
margin: 0px;
padding: 0px;
}
/**
* 单独设置mapid为100%不显示,可能float坍塌
*/
html,
body,
#mapDiv {
height: 100%;
width: 100%;
}
</style>
</head>
<body>
<div id="mapDiv">
</div>
</body>
<script>
//地图地址
var url = 'https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw';
var attr = ' Map data &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, &copy; <a href="http://cartodb.com/attributions">CartoDB</a>';
var leafletMap = L.map('mapDiv').setView([39.7562, -104], 6);
//图层
L.tileLayer(url, {
maxZoom: 18,
attribution: attr,
id: 'mapbox.streets'
}).addTo(leafletMap);
//标记点
var geojsonFeature = {
"type": "Feature",
"properties": {
"name": "Coors Field",
"amenity": "Baseball Stadium",
"popupContent": "This is where the Rockies play!"
},
"geometry": {
"type": "Point",
"coordinates": [-104.99404, 39.75621]
}
};
L.geoJSON(geojsonFeature).addTo(leafletMap);
var myLines = [{
"type": "LineString",
"coordinates": [[-100, 40], [-105, 45], [-110, 55]]
}, {
"type": "LineString",
"coordinates": [[-105, 40], [-110, 45], [-115, 55]]
}];
// 这么操作也可以
var myLayer = L.geoJSON().addTo(leafletMap);
myLayer.addData(myLines);
// 定义风格
var myStyle = {
"color": "#ff7800",
"weight": 5,
"opacity": 0.65
};
L.geoJSON(myLines, {
style: myStyle
}).addTo(leafletMap);
// 根据geojson的属性定义显示样式
var states = [{
"type": "Feature",
"properties": {"party": "Republican"},
"geometry": {
"type": "Polygon",
"coordinates": [[
[-104.05, 48.99],
[-97.22, 48.98],
[-96.58, 45.94],
[-104.03, 45.94],
[-104.05, 48.99]
]]
}
}, {
"type": "Feature",
"properties": {"party": "Democrat"},
"geometry": {
"type": "Polygon",
"coordinates": [[
[-109.05, 41.00],
[-102.06, 40.99],
[-102.03, 36.99],
[-109.04, 36.99],
[-109.05, 41.00]
]]
}
}];
L.geoJSON(states, {
style: function(feature) {
switch (feature.properties.party) {
case 'Republican': return {color: "#ff0000"};
case 'Democrat': return {color: "#0000ff"};
}
}
}).addTo(map);
</script>
</html>
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/n7gs7/leaflet-book.git
git@gitee.com:n7gs7/leaflet-book.git
n7gs7
leaflet-book
leaflet-book
master

搜索帮助