-
Notifications
You must be signed in to change notification settings - Fork 0
/
Version 0.1
344 lines (288 loc) · 10.6 KB
/
Version 0.1
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<!DOCTYPE html>
<html>
<head>
<!-- Obtained from https://developers.google.com/maps/documentation/javascript/examples/places-searchbox -->
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Places Search Box</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;
}
#description {
font-family: Roboto;
font-size: 15px;
font-weight: 300;
}
#infowindow-content .title {
font-weight: bold;
}
#infowindow-content {
display: none;
}
#map #infowindow-content {
display: inline;
}
.pac-card {
margin: 10px 10px 0 0;
border-radius: 2px 0 0 2px;
box-sizing: border-box;
-moz-box-sizing: border-box;
outline: none;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
background-color: #fff;
font-family: Roboto;
}
#pac-container {
padding-bottom: 12px;
margin-right: 12px;
}
.pac-controls {
display: inline-block;
padding: 5px 11px;
}
.pac-controls label {
font-family: Roboto;
font-size: 13px;
font-weight: 300;
}
#pac-input {
background-color: #fff;
font-family: Roboto;
font-size: 15px;
font-weight: 300;
margin-left: 12px;
padding: 0 11px 0 13px;
text-overflow: ellipsis;
width: 400px;
}
#pac-input:focus {
border-color: #4d90fe;
}
<!-- This relates to the second search bar. -->
.pac-card2 {
margin: 10px 10px 0 0;
border-radius: 2px 0 0 2px;
box-sizing: border-box;
-moz-box-sizing: border-box;
outline: none;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
background-color: #fff;
font-family: Roboto;
}
#pac-container2 {
padding-bottom: 12px;
margin-right: 12px;
}
.pac-controls2 {
display: inline-block;
padding: 5px 11px;
}
.pac-controls2 label {
font-family: Roboto;
font-size: 13px;
font-weight: 300;
}
#pac-input2 {
background-color: #fff;
font-family: Roboto;
font-size: 15px;
font-weight: 300;
margin-left: 12px;
padding: 0 11px 0 13px;
text-overflow: ellipsis;
width: 400px;
}
#pac-input2:focus {
border-color: #4d90fe;
}
#title {
color: #fff;
background-color: #4d90fe;
font-size: 25px;
font-weight: 500;
padding: 6px 12px;
}
#target {
width: 345px;
}
</style>
</head>
<body>
<!-- The first search bar. --->
<input id="pac-input" class="controls" type="text" placeholder="Search Address">
<!-- The Current Location Button. --->
<button class="LocationButton" onclick="getLocation()">Current Location</button>
<!-- Small text to screen used for testing. Will be deleted later. --->
<p id="demo">Test</p>
<!-- The Second search bar. --->
<input id="pac-input2" class="controls2" type="text" placeholder="Search Address">
<div id="map"></div>
<script>
// This example adds a search box to a map, using the Google Place Autocomplete
// feature. People can enter geographical searches. The search box will return a
// pick list containing a mix of places and predicted search terms.
// This example requires the Places library. Include the libraries=places
// parameter when you first load the API. For example:
// <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">
function initAutocomplete() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 38.251767, lng: -85.753965},
zoom: 13,
mapTypeId: 'roadmap'
});
// Variables for the data inside the search boxes.
var input = document.getElementById('pac-input');
var input2 = document.getElementById('pac-input2');
// SearchBox for users.
var searchBox = new google.maps.places.SearchBox(input);
var searchBox2 = new google.maps.places.SearchBox(input2);
// Posision on where SearchBoxs are kept.
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input2);
// Bias the SearchBox results towards current map's viewport.
map.addListener('bounds_changed', function() {
searchBox.setBounds(map.getBounds());
searchBox2.setBounds(map.getBounds())
});
var markers = [];
// Listen for the event fired when the user selects a prediction and retrieve
// more details for that place.
searchBox.addListener('places_changed', function() {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
// Clear out the old markers.
markers.forEach(function(marker) {
marker.setMap(null);
});
markers = [];
// For each place, get the icon, name and location.
var bounds = new google.maps.LatLngBounds();
places.forEach(function(place) {
if (!place.geometry) {
console.log("Returned place contains no geometry");
return;
}
var icon = {
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25)
};
// Create a marker for each place.
markers.push(new google.maps.Marker({
map: map,
icon: icon,
title: place.name,
position: place.geometry.location
}));
if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
});
var markers2 = [];
// Adding listener for when the user enters text into the search box.
searchBox2.addListener('places_changed', function() {
var places2 = searchBox2.getPlaces();
if (places2.length == 0) {
return;
}
// Clear out the old markers2.
markers2.forEach(function(marker) {
marker.setMap(null);
});
markers2 = [];
// For each place, get the icon, name and location.
var bounds = new google.maps.LatLngBounds();
places2.forEach(function(places2) {
if (!places2.geometry) {
console.log("Returned places2 contains no geometry");
return;
}
var icon = {
url: places2.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25)
};
// Create a marker for each places2.
markers2.push(new google.maps.Marker({
map: map,
icon: icon,
title: places2.name,
position: places2.geometry.location
}));
if (places2.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(places2.geometry.viewport);
} else {
bounds.extend(places2.geometry.location);
}
});
map.fitBounds(bounds);
});
} // End of function initAutocomplete();
<!-- Function that is called when the btn is clicked, asks for permission then calls another function. -->
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(CapturePosition);
}
else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
// Function used from the button to capture the lat and lon of the user then puts them into global variables.
function CapturePosition(position) {
Geolocation_Lat = position.coords.latitude;
Geolocation_Long = position.coords.longitude;
CustomMarker = false; // Sets it to false to fall into other tree.
initAutocomplete(); // runs the function to re-pin onto the map using the users location instead of user's subbmission.
};
// This code will take the lat and lon from the user then convert it into gelocation for the initAutocomplete to use.
/***************************************************************************************************************************
This is where the problem comes into place, I have to use the geocoder, map and infowindow. Whenever The function is called
this will bring out the error: Cannot read property 'ownerDocument' of null, Im unsure if its becasue of geocoder, I tried
to take what geocoder had and place it into a placeholder variable to later be used over and over but that failed, I also
thing somthing doesnt work correctly when it calls map again and redesigns the map.
***************************************************************************************************************************/
function geocodeLatLng(geocoder, map, infowindow) {
var latlng = {lat: parseFloat(Geolocation_Lat[0]), lng: parseFloat(Geolocation_Long[1])};
geocoder.geocode({'location': latlng}, function(results, status) {
if (status === 'OK') {
if (results[0]) {
map.setZoom(13);
var marker = new google.maps.Marker({
position: latlng,
map: map
});
infowindow.setContent(results[0].formatted_address);
infowindow.open(map, marker);
} else {
window.alert('No results found');
}
} else {
window.alert('Geocoder failed due to: ' + status);
}
})};
</script>
<script src="https://maps.googleapis.com/maps/api/js?key= _Google API Key__ &libraries=places&callback=initAutocomplete"
async defer></script>
</body>
</html>