-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolyline.html
More file actions
215 lines (185 loc) · 7.19 KB
/
polyline.html
File metadata and controls
215 lines (185 loc) · 7.19 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Simple Polyline, Polygon - Google Maps API demo - August - Let's Write</title>
<link rel="canonical" href="https://www.letswrite.tw/google-map-api-geometry/">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<style>
.container {
padding-top: 30px;
padding-bottom: 30px;
}
#map {
background: #CCC;
}
.fixed-bottom {
position: fixed;
left: 16px;
bottom: 0;
max-width: 320px;
}
</style>
<link rel="shortcut icon" href="https://letswritetw.github.io/letswritetw/dist/img/logo_512.png"/>
<!-- Google Tag Manager-->
<script>
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-PGQ9WQT');
</script>
</head>
<body>
<!-- Google Tag Manager (noscript)-->
<noscript>
<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-PGQ9WQT" height="0" width="0" style="display:none;visibility:hidden"></iframe>
</noscript>
<div id="app" class="container">
<div class="row">
<div class="col">
<div id="map" class="embed-responsive embed-responsive-16by9"></div>
</div>
</div>
<div class="row fixed-bottom">
<div class="col">
<div class="alert alert-info" role="alert">
Let's Write 筆記文:<br/>
<a href="https://letswrite.tw/google-map-api-geometry/" target="_blank">Google Maps API學習筆記-2:在地圖上畫個日本結界</a>
</div>
</div>
</div>
</div>
<!-- 將 YOUR_API_KEY 替換成你的 API Key 即可 -->
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY" async defer></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.min.js"></script>
<!-- map -->
<script>
const googleMap = new Vue({
el: '#app',
data: {
map: null,
simplePolyline: [], // 存入折線圖的點
simplePolygon: [] // 存入多邊形的點
},
methods: {
// init google map
initMap() {
// 預設顯示的地點:靖國神社
let location = {
lat: 35.694136, // 經度
lng: 139.743849 // 緯度
};
// 建立地圖
this.map = new google.maps.Map(document.getElementById('map'), {
center: location,
zoom: 13,
mapTypeId: 'terrain'
});
// 畫折線圖:平將門北斗七星
fetch('./big-dipper.geojson')
.then(results => results.json())
.then(result => {
let res = result.features;
let flightPlanCoordinates = [];
Array.prototype.forEach.call(res, r => {
let latLng = new google.maps.LatLng(r.geometry.coordinates[0], r.geometry.coordinates[1]);
let marker = new google.maps.Marker({
position: latLng,
map: this.map
});
// info window
let infowindow = new google.maps.InfoWindow({
content: `<span class="font-weight-bold">${r.properties.name}</span>` // 支援html
});
infowindow.open(this.map, marker);
// 將每個折線的點存入 simplePolyline
this.simplePolyline.push({
lat: r.geometry.coordinates[0],
lng: r.geometry.coordinates[1]
});
});
// 畫折線圖
let polyline = new google.maps.Polyline({
path: this.simplePolyline,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 5
});
polyline.setMap(this.map);
});
// 畫多邊形:天皇正方形
fetch('./polygon.geojson')
.then(results => results.json())
.then(result => {
let res = result.features;
let flightPlanCoordinates = [];
Array.prototype.forEach.call(res, r => {
let latLng = new google.maps.LatLng(r.geometry.coordinates[0], r.geometry.coordinates[1]);
let marker = new google.maps.Marker({
position: latLng,
map: this.map
});
// info window
let infowindow = new google.maps.InfoWindow({
content: `<span class="font-weight-bold">${r.properties.name}</span>` // 支援html
});
infowindow.open(this.map, marker);
// 將每個正方形的點存入 simplePolygon
this.simplePolygon.push({
lat: r.geometry.coordinates[0],
lng: r.geometry.coordinates[1]
});
});
// 刪掉最後一個正方形中心點的座標
this.simplePolygon.pop();
// 畫多邊形圖
var bermudaTriangle = new google.maps.Polygon({
paths: this.simplePolygon,
strokeColor: '#f1c40f',
strokeOpacity: 0.8,
strokeWeight: 5,
fillColor: '#f1c40f',
fillOpacity: 0.24
});
bermudaTriangle.setMap(this.map);
});
// 補多邊形的折線
let polylineLeft = new google.maps.Polyline({
path: [
{ lat: 35.7229985, lng: 139.7198027 },
{ lat: 35.694136, lng: 139.743849 },
{ lat: 35.6667348, lng: 139.7718534 }
],
geodesic: true,
strokeColor: '#f1c40f',
strokeOpacity: 1.0,
strokeWeight: 5
});
polylineLeft.setMap(this.map);
let polylineRight = new google.maps.Polyline({
path: [
{ lat: 35.7242569, lng: 139.7716722 },
{ lat: 35.694136, lng: 139.743849 },
{ lat: 35.667028, lng: 139.7219214 }
],
geodesic: true,
strokeColor: '#f1c40f',
strokeOpacity: 1.0,
strokeWeight: 5
});
polylineRight.setMap(this.map);
}
},
created() {
window.addEventListener('load', () => {
this.initMap();
});
}
});
</script>
</body>
</html>