-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmap.html
More file actions
220 lines (202 loc) · 7.93 KB
/
map.html
File metadata and controls
220 lines (202 loc) · 7.93 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
216
217
218
219
220
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0">
<meta charset="utf-8">
<title>Pokemon go controller</title>
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
.controls {
margin-top: 10px;
height: 25px;
outline: none;
margin-left: 20px;
}
#pac-input {
background-color: #fff;
font-family: Roboto;
font-size: 15px;
font-weight: 300;
margin-left: 12px;
margin-top:10px;
padding: 0 11px 0 13px;
text-overflow: ellipsis;
width: 300px;
height:25px;
}
#pac-input:focus {
border-color: #4d90fe;
}
.button {
background-color: #4CAF50; /* Green */
border: 1px solid green;
color: white;
padding: 5px 5px 6px 5px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 13px;
cursor: pointer;
float: left;
}
.button:hover {
background-color: #3e8e41;
}
</style>
<script
src="https://code.jquery.com/jquery-3.1.0.min.js"
integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s="
crossorigin="anonymous"></script> <!-- https://code.jquery.com //-->
<script>
var map, greenCircle, redCircle;
var Markers = [];
function measure(lat1, lng1, lat2, lng2){ // generally used geo measurement function
// http://stackoverflow.com/questions/639695/how-to-convert-latitude-or-longitude-to-meters
var R = 6378.137; // Radius of earth in KM
var dLat = (lat2 - lat1) * Math.PI / 180;
var dLon = (lng2 - lng1) * Math.PI / 180;
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
return d * 1000; // meters
}
function removeAllMarkers() {
Markers.forEach(function(element, index, array) {
if (array.length - 1 !== index) {
element.setMap(null);
}
})
}
function addMarker(latLng) {
var marker = new google.maps.Marker({
position: latLng,
map: map,
draggable: false
});
marker.addListener('click', function(event) {
clickAction(event);
});
Markers.push(marker);
map.panTo(latLng);
moveCircle(latLng);
}
function putServer(latLng) {
$.post("/", latLng).done(
function() {
addMarker(latLng);
}
);
}
function moveCircle(latLng) {
greenCircle.setCenter(latLng);
redCircle.setCenter(latLng);
}
function initCircle() {
greenCircle = new google.maps.Circle({
strokeColor: '#009900',
strokeOpacity: 0.5,
strokeWeight: 1,
fillColor: '#009900',
fillOpacity: 0.3,
map: map,
radius: 10, // 10 meter
});
redCircle = new google.maps.Circle({
strokeColor: '#990000',
strokeOpacity: 0.5,
strokeWeight: 1,
fillColor: '#990000',
fillOpacity: 0.3,
map: map,
radius: 30, // 30 meter
});
redCircle.addListener('click', function(event) {
clickAction(event);
});
}
function showDistance(latLng) {
if (Markers.length === 0) {
return;
}
oldLatLng = Markers[Markers.length - 1];
var lat1 = oldLatLng.position.lat();
var lng1 = oldLatLng.position.lng();
var lat2 = latLng.lat;
var lng2 = latLng.lng;
var meters = measure(lat1, lng1, lat2, lng2);
console.log(lat2 + ' ' + lng2 + ' : ' + meters.toFixed(1) + 'm');
}
function clickAction(event) {
var latLng = {lat: event.latLng.lat(), lng: event.latLng.lng()};
showDistance(latLng);
putServer(latLng);
}
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 19,
heading: 10,
tilt: 45,
mapTypeId: google.maps.MapTypeId.SATELLITE
});
var input = /** @type {!HTMLInputElement} */(document.getElementById('pac-input'));
var buttonContainer = document.getElementById('button-container');
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(buttonContainer);
var autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.bindTo('bounds', map);
autocomplete.addListener('place_changed', function() {
var place = autocomplete.getPlace();
if (!place.geometry) {
window.alert("Autocomplete's returned place contains no geometry");
return;
}
map.setCenter(place.geometry.location);
});
var params={};
window.location.search.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(str,key,value) {
params[key] = value;
});
initCircle();
if (! $.isEmptyObject(params) && confirm('Do you using last location?')) {
addMarker({
lat: parseFloat(params['lat']),
lng: parseFloat(params['lon'])
});
} else {
geocoder = new google.maps.Geocoder;
navigator.geolocation.getCurrentPosition(function(position) {
addMarker({
lat: position.coords.latitude,
lng: position.coords.longitude
});
});
}
map.addListener('click', function(event) {
clickAction(event);
});
$( "#remove-all-markers" ).click(function() {
removeAllMarkers();
});
}
</script>
<script
src="https://maps.googleapis.com/maps/api/js?libraries=places&key={{GOOGLE_MAP_API_KEY}}&callback=initMap"
async defer></script>
</head>
<body>
<input id="pac-input" class="controls" type="text" placeholder="Enter a location">
<div id="button-container" class="controls">
<button class="button" id="remove-all-markers">Remove All markers</button>
</div>
<div id="map"></div>
</body>
</html>