GIS工具maptalks开发手册(五)01-用JSON载入地图——json格式绘制多个面之基础版
效果-json渲染图层基础版
代码
index.html
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>JSON序列化 - 用JSON载入地图</title>
<style type="text/css">
html,
body {
margin: 0px;
height: 100%;
width: 100%;
}
.container {
width: 900px;
height: 500px;
margin: 50px;
}
#json {
position: fixed;
background-color: rgba(13, 13, 13, 0.5);
padding: 10px 10px 10px 10px;
font: 13px bold sans-serif;
color: #fff;
left: 0px;
top: 0px;
width: 100%;
height: 85px;
overflow: hidden
}
</style>
<link rel="stylesheet" href="https://unpkg.com/maptalks/dist/maptalks.css">
<script type="text/javascript" src="https://unpkg.com/maptalks/dist/maptalks.min.js"></script>
<body>
<div id="map" class="container"></div>
<!-- <div id="json"></div> -->
<script>
var mapJSON = {
"version": "1.0",
"options": {
"center": {
"x": 114.40178858433023,
"y": 37.00265895531547
},
"zoom": 19
},
"baseLayer": {
"type": "TileLayer",
"id": "base",
"options": {
"urlTemplate": "http://{s}.tile.osm.org/{z}/{x}/{y}.png",
"subdomains": ["a", "b", "c"]
}
},
"layers": [{
"type": "VectorLayer",
"id": "v",
"geometries": [{
"feature": {
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [[114.40154986772791, 37.002687616434656], [114.40156562570587, 37.00268788429551], [114.40156529042974, 37.00268440210405], [114.40154919717565, 37.0026841342432]]
}
},
symbol: [{
lineColor: '#34495e',
lineWidth: 2,
polygonFill: '#fef102',
polygonOpacity: 0.6
},
{
'markerDy': 20,
'textFaceName': 'sans-serif',
'textName': '李世民',
'textFill': '#000',
'textHorizontalAlignment': 'center',
'textSize': 16,
'textDx': 0,
'textDy': 16,
}]
}, {
"feature": {
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [[114.40178858433023, 37.00265895531547], [114.40180333647982, 37.00265895531547], [114.40180333647982, 37.002656544566634], [114.40180735979334, 37.002656276705665], [114.40180769506946, 37.002647705153635], [114.4017882490541, 37.00264743729261], [114.4017882490541, 37.002647705153635]]
}
},
symbol: [{
lineColor: '#34495e',
lineWidth: 2,
polygonFill: '#01a2e6',
polygonOpacity: 0.6
},
{
'markerDy': 20,
'textFaceName': 'sans-serif',
'textName': '曹操',
'textFill': '#000',
'textHorizontalAlignment': 'center',
'textSize': 16,
'textDx': 0,
'textDy': 7,
}]
}, {
"feature": {
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [[114.40157836619869, 37.00256520391568], [114.40158440116898, 37.00256520391568], [114.4015833953406, 37.00257511478341], [114.40171147082106, 37.00257457906085], [114.40171180609718, 37.0025769898123], [114.40177953187481, 37.00257752553486], [114.40177919659868, 37.00256466819306], [114.40178389046446, 37.00256520391568], [114.40178422574058, 37.00255984668931], [114.40177852604643, 37.00255984668931], [114.4017781907703, 37.002548060790076], [114.40171415303007, 37.00254752506734], [114.40171281192556, 37.00255020368101], [114.40158440116898, 37.00254913223554], [114.40158406589285, 37.00255984668931], [114.40157836619869, 37.00255931096669]]
}
},
symbol: [{
lineColor: '#34495e',
lineWidth: 2,
polygonFill: '#01a2e6',
polygonOpacity: 0.6
},
{
'markerDy': 20,
'textFaceName': 'sans-serif',
'textName': '朱元璋',
'textFill': '#000',
'textHorizontalAlignment': 'center',
'textSize': 16,
'textDx': 0,
'textDy': 7,
}]
}]
}]
};
var map = maptalks.Map.fromJSON('map', mapJSON);
var layer = map.getLayer('v');
var drawTool = new maptalks.DrawTool({
mode: 'Point'
}).addTo(map).disable();
drawTool.on('drawend', function (param) {
// console.log(param.geometry);
console.log('面的坐标', param.geometry._coordinates);
layer.addGeometry(param.geometry);
});
var items = ['Point', 'LineString', 'Polygon'].map(function (value) {
return {
item: value,
click: function () {
drawTool.setMode(value).enable();
}
};
});
var toolbar = new maptalks.control.Toolbar({
items: [
{
item: 'Shape/绘制',
children: items
},
{
item: 'Disable关闭',
click: function () {
drawTool.disable();
}
},
{
item: 'Clear清空',
click: function () {
layer.clear();
}
}
]
}).addTo(map);
// document.getElementById('json').innerHTML = JSON.stringify(mapJSON);
</script>
</body>
</html>