-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathworldmap.html
146 lines (128 loc) · 4.42 KB
/
worldmap.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>World Map</title>
<style>
.map_path {
stroke: black;
stroke-width: 1;
}
.grid_path{
stroke: gray;
stroke-width: 1;
fill:white;
opacity: 1;
}
</style>
<!-- Bootstrap core CSS -->
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row">
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title">世界地图</h3>
</div>
<div class="panel-body">
<div class="col-sm-8">
<div id="map"></div>
</div>
<div class="col-sm-4">
<form role="form">
<div class="form-group">
<label>投影</label>
<select class="form-control" id="select-map-projection">
<option value="1">墨卡托投影(等角正切圆柱投影)</option>
<option value="2">方位角直角投影</option>
<option value="3">圆锥正形投影</option>
<option value="4">方位角立体投影</option>
<option value="5">等角投影(普通圆柱投影)</option>
<option value="6">球心投影</option>
<option value="7">横向墨卡托投影</option>
</select>
</div>
<div class="form-group">
<label>比例尺<font style="color: gray">(设置不同的比例尺,获取更好的展示效果)</font></label>
<input id="map-scale" class="form-control" value="150"/>
</div>
<div class="form-group pull-right">
<button id="btn-draw-map" type="button" class="btn btn-lg btn-success" >绘制</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
function drawEarth ( projection ){
var width = 754; var height = 754;
var svg = d3.select("#map").append("svg")
.attr("width", width)
.attr("height", height);
var graticule = d3.geoGraticule();
var path = d3.geoPath()
.projection(projection);
d3.json("world.json", function(error, root) {
if (error)
return console.error(error);
var grid = graticule();
var map = svg.append("g")
.attr("transform", "translate(" + -100 + "," + 40 + ")");
map.append("path")
.datum( grid )
.attr("id","grid_id")
.attr("class","grid_path")
.attr("d",path);
map.selectAll(".map_path")
.data( root.features )
.enter()
.append("path")
.attr("class","map_path")
.attr("fill",function(d,i){
return "#81DBC6";
}).attr("d", path );
});
}
$(document).ready(function(){
drawEarth(d3.geoMercator().scale(120));
$("#btn-draw-map").click(function () {
var selprojection = $("#select-map-projection").children('option:selected').val();
var mapScale = parseInt($("#map-scale").val());
var projection = null;
switch (parseInt(selprojection)){
case 1:
projection = d3.geoMercator().scale(mapScale);
break;
case 2:
projection = d3.geoOrthographic().scale(mapScale);
break;
case 3:
projection = d3.geoConicConformal().scale(mapScale);
break;
case 4:
projection = d3.geoStereographic().scale(mapScale);
break;
case 5:
projection = d3.geoEquirectangular().scale(mapScale);
break;
case 6:
projection = d3.geoGnomonic().scale(mapScale);
break;
case 7:
projection = d3.geoTransverseMercator().scale(mapScale);
break;
}
$("#map").empty();
if ( projection != null){
drawEarth(projection);
}
});
});
</script>
</body>
</html>