-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.js
54 lines (48 loc) · 1.91 KB
/
search.js
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
let autocomplete;
function initAutocomplete() {
const input = document.getElementById('search-input');
const searchButton = document.getElementById('search-button');
autocomplete = new google.maps.places.Autocomplete(input, {
types: ['address'],
componentRestrictions: {country: 'us'},
bounds: new google.maps.LatLngBounds(
new google.maps.LatLng(26.0, -98.5),
new google.maps.LatLng(26.4, -98.0)
),
strictBounds: true
});
autocomplete.addListener('place_changed', performSearch);
searchButton.addEventListener('click', performSearch);
input.addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
performSearch();
}
});
}
function performSearch() {
const place = autocomplete.getPlace();
if (!place || !place.geometry) {
const service = new google.maps.places.AutocompleteService();
service.getPlacePredictions({ input: document.getElementById('search-input').value }, function(predictions, status) {
if (status === google.maps.places.PlacesServiceStatus.OK && predictions && predictions.length > 0) {
const placesService = new google.maps.places.PlacesService(document.createElement('div'));
placesService.getDetails({ placeId: predictions[0].place_id }, function(result, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
handleSelectedPlace(result);
}
});
} else {
console.log("No results found");
}
});
} else {
handleSelectedPlace(place);
}
}
function handleSelectedPlace(place) {
const latlng = [place.geometry.location.lat(), place.geometry.location.lng()];
addCustomMarker(latlng);
map.setView(latlng, 16);
showNearbyLocations(latlng);
}
initAutocomplete();