Guest User

Untitled

a guest
Mar 18th, 2018
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. <input id="origin" type="text" placeholder="Search Box">
  2. <input id=submit type="submit" value="Calculate"/>
  3. <div id="map"></div>
  4.  
  5. function initAutocomplete() {
  6. var map = new google.maps.Map(document.getElementById('map'), {
  7. center: {lat: 65.049686, lng: 15.957848},
  8. zoom: 5,
  9. mapTypeId: 'roadmap'
  10. });
  11. // Create the search box and link it to the UI element.
  12. var input = document.getElementById('origin');
  13. var searchBox = new google.maps.places.SearchBox(input);
  14.  
  15.  
  16. // Bias the SearchBox results towards current map's viewport.
  17. map.addListener('bounds_changed', function() {
  18. searchBox.setBounds(map.getBounds());
  19. });
  20.  
  21. var markers = [];
  22. // Listen for the event fired when the user selects a prediction and retrieve
  23. // more details for that place.
  24. searchBox.addListener('places_changed', function() {
  25. var places = searchBox.getPlaces();
  26. if (places.length == 0) {
  27. return;
  28. }
  29.  
  30. // Clear out the old markers.
  31. markers.forEach(function(marker) {
  32. marker.setMap(null);
  33.  
  34. });
  35. markers = [];
  36.  
  37. // For each place, get the icon, name and location.
  38. var bounds = new google.maps.LatLngBounds();
  39. places.forEach(function(place) {
  40. if (!place.geometry) {
  41. console.log("Returned place contains no geometry");
  42. return;
  43. }
  44. var icon = {
  45. url: place.icon,
  46. size: new google.maps.Size(71, 71),
  47. origin: new google.maps.Point(0, 0),
  48. anchor: new google.maps.Point(17, 34),
  49. scaledSize: new google.maps.Size(25, 25)
  50. };
  51.  
  52. // Create a marker for each place.
  53. markers.push(new google.maps.Marker({
  54. map: map,
  55. icon: icon,
  56. title: place.name,
  57. position: place.geometry.location
  58. }));
  59.  
  60. if (place.geometry.viewport) {
  61. // Only geocodes have viewport.
  62. bounds.union(place.geometry.viewport);
  63. } else {
  64. bounds.extend(place.geometry.location);
  65. }
  66. });
  67. map.fitBounds(bounds);
  68. map.setZoom (8);
  69.  
  70. });
  71. }
Add Comment
Please, Sign In to add comment