diff --git a/static/map.js b/static/map.js index 48cfa784..65c23676 100644 --- a/static/map.js +++ b/static/map.js @@ -146,9 +146,56 @@ function initMap() { }); } + initSearchBox(); initGeoLocation(); }; +function initSearchBox() { + // Create the search box and link it to the UI element. + var input = document.getElementById('pac-input'); + var searchBox = new google.maps.places.SearchBox(input); + map.controls[google.maps.ControlPosition.TOP_RIGHT].push(input); + + // Bias the SearchBox results towards current map's viewport. + map.addListener('bounds_changed', function() { + searchBox.setBounds(map.getBounds()); + }); + + // 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; + } + + // 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) + }; + + if (place.geometry.viewport) { + // Only geocodes have viewport. + bounds.union(place.geometry.viewport); + } else { + bounds.extend(place.geometry.location); + } + }); + map.fitBounds(bounds); + }); +}; + function initGeoLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(function(position) { diff --git a/templates/map.html b/templates/map.html index 187fd335..d490a0f2 100644 --- a/templates/map.html +++ b/templates/map.html @@ -273,6 +273,7 @@

Settings

+
@@ -284,6 +285,6 @@

Settings

- +