Created
January 27, 2020 19:24
-
-
Save anyelopetit/f371622acad6bf5ff2481e4867fc4643 to your computer and use it in GitHub Desktop.
Google Map script with places autocomplete field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var gMapObj = gMapObj || {}; | |
var gMapObjMarker = gMapObjMarker || {}; | |
var map = null; | |
var marker = null; | |
var afterGmapLoaded = function(){ | |
function setMap(lat, lng) { | |
map = new google.maps.Map(document.getElementById("account_latitude_longitude"), { | |
zoom: 14, | |
center: new google.maps.LatLng(lat, lng), | |
mapTypeId: google.maps.MapTypeId.ROADMAP | |
}); | |
if (!map) { | |
setTimeout(setMap, 50) | |
} | |
google.maps.event.addListenerOnce(map, 'bounds_changed', function() { | |
console.log(map.getBounds()); | |
}); | |
gMapObj = map; | |
marker = new google.maps.Marker({ | |
position: new google.maps.LatLng(lat, lng), | |
map: map | |
}); | |
gMapObjMarker = marker; | |
updateLocation(map.center); | |
google.maps.event.addListener(map, 'click', function(e) { | |
updateLocation(e.latLng); | |
}); | |
$("#account_latitude").val(lat); | |
$("#account_longitude").val(lng); | |
autocomplete.bindTo('bounds', map); | |
autocomplete.addListener('place_changed', function() { | |
var place = autocomplete.getPlace(); | |
var location = place.geometry.location; | |
updateLocation(location) | |
return; | |
}); | |
} | |
function success(pos) { | |
var crd = pos.coords; | |
setMap(crd.latitude, crd.longitude) | |
}; | |
function error(err) { | |
console.warn('ERROR(' + err.code + '): ' + err.message); | |
setMap(40.4193145, -3.6960273) | |
}; | |
var options = { | |
enableHighAccuracy: true, | |
timeout: 5000, | |
maximumAge: 0 | |
}; | |
if (navigator.geolocation) { | |
navigator.geolocation.getCurrentPosition(success, error, options) | |
} else { | |
alert("Sorry, browser does not support geolocation!"); | |
} | |
var input = document.getElementById('searchInput'); | |
var autocomplete = new google.maps.places.Autocomplete(input); | |
function updateLocation(location) { | |
if(marker) { | |
marker.setPosition(location); | |
} else { | |
marker = new google.maps.Marker({ | |
position: location, | |
map: map | |
}); | |
} | |
gMapObjMarker = marker; | |
map.setCenter(location); | |
$("#account_latitude").val(location.lat()); | |
$("#account_longitude").val(location.lng()); | |
} | |
} | |
if (typeof window.google == 'undefined' || typeof google.maps == 'undefined'){ | |
$.getScript('http://maps.googleapis.com/maps/api/js?key=#{Rails.application.secrets.google_api_key}', function(){ | |
// afterGmapLoaded() | |
google.maps.event.addDomListener(window, 'load', afterGmapLoaded); | |
}); | |
} else { | |
// afterGmapLoaded(); | |
google.maps.event.addDomListener(window, 'load', afterGmapLoaded); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment