-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
212 lines (184 loc) · 6.09 KB
/
index.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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title>Finding Nemo</title>
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
.inline {
display: inline-block
}
.gmnoprint {
display: none
}
</style>
</head>
<body>
<div class="inline col-md-2 col-5">
<input id="pac-input" class="form-control" type="text" placeholder="Place 1">
</div>
<div class="inline col-md-2 col-5">
<input id="pac-input2" class="form-control" type="text" placeholder="Place 2">
</div>
<div class="inline col-md-2 col-5">
<input id="pac-input3" class="form-control" type="text" placeholder="Place 3">
</div>
<div class="inline col-md-2 col-5">
<input id="pac-input3" class="form-control" type="text" placeholder="Place 4">
</div>
<!-- <button type="button" id="btnAdd" class="btn btn-outline-primary" onclick="addInput()">Add Place</button> -->
<div id="map"></div>
<script>
var myLatLng;
var distance;
var place1, place2;
var circle, circle2;
var polygon;
var searchBox = [];
var markers = [];
var hasData = [];
var triangleCoords = [];
function initMap() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
myLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
// Create the map.
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 13,
center: myLatLng,
});
var btnAdd = document.getElementById('btnAdd');
map.controls[google.maps.ControlPosition.TOP_LEFT].push(btnAdd);
var inputs = document.getElementsByClassName('form-control');
for (let index = 0; index < inputs.length; index++) {
const e = inputs[index];
searchBox[index] = new google.maps.places.SearchBox(e);
map.addListener('bounds_changed', function () {
searchBox[index].setBounds(map.getBounds());
});
searchBox[index].addListener('places_changed', function () {
var places = searchBox[index].getPlaces();
if (places.length == 0) {
return;
}
// Clear out the old markers.
if (hasData[index] == true) {
markers[index].setMap(null);
markers.splice(index, 1);
}
places.forEach(function (place) {
if (!place.geometry) {
console.log("Returned place contains no geometry");
return;
}
hasData[index] = true
//Create a marker for each place.
markers.push(new google.maps.Marker({
map: map,
title: place.name,
position: place.geometry.location
}));
displayRadius(markers)
});
});
};
function displayRadius(markers) {
if (markers.length < 2)
return;
clearAll()
if (markers.length == 2) {
place1 = markers[0].position
place2 = markers[1].position
distance = google.maps.geometry.spherical.computeDistanceBetween(place1, place2)
// Add the circle for this
circle = new google.maps.Circle({
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 1,
fillColor: '#FF0000',
fillOpacity: 0.1,
map: map,
center: place1,
radius: distance
});
circle2 = new google.maps.Circle({
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 1,
fillColor: '#FF0000',
fillOpacity: 0.1,
map: map,
center: place2,
radius: distance
});
} else {
triangleCoords = [];
markers.forEach(m => {
triangleCoords.push(m.position)
});
polygon = new google.maps.Polygon({
paths: triangleCoords,
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 1,
fillColor: '#FF0000',
fillOpacity: 0.1
});
polygon.setMap(map);
}
// fit center of screen
var bounds = new google.maps.LatLngBounds();
markers.forEach(m => {
bounds.extend(m.position)
});
map.fitBounds(bounds);
}
function clearAll() {
if (circle != null)
circle.setMap(null);
if (circle2 != null)
circle2.setMap(null);
if (polygon != null)
polygon.setMap(null);
}
}, function error(msg) {
alert('Please enable your GPS.');
}, {
maximumAge: 600000,
//timeout: 5000,
enableHighAccuracy: true
});
}
}
// function addInput() {
// $('#myForm').append('<div class="inline col-md-2 col-12"><input id="pac-input" class="form-control" type="text" placeholder="Place ..."></div>');
// };
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBMZKcVJJ0FCXeUyYYsVl5_Y22aIOgTAxQ&callback=initMap&libraries=geometry,places®ion=VN"
type="text/javascript"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
crossorigin="anonymous"></script>
</body>
</html>