Created
January 4, 2020 10:48
-
-
Save sungraiz/9eb9aee883dc0995cd9655f709e9dd99 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // Create the autocomplete object, restricting the search predictions to | |
| // geographical location types. | |
| var autocomplete = new google.maps.places.Autocomplete( | |
| document.getElementById(addressField), {types: ['geocode']}); | |
| // Avoid paying for data that you don't need by restricting the set of | |
| // place fields that are returned to just the address components. | |
| autocomplete.setFields(['geometry']); | |
| // When the user selects an address from the drop-down, populate the | |
| // address fields in the form. | |
| autocomplete.addListener('place_changed', () => { | |
| var place = autocomplete.getPlace(); | |
| var lat = place.geometry.location.lat(), | |
| lng = place.geometry.location.lng(); | |
| that.container.find(".map-coordinate").val(lat + ',' + lng ); | |
| var location = new window.google.maps.LatLng(lat, lng); | |
| that.map.setCenter(location); | |
| // Drop the Marker | |
| setTimeout(function(){ | |
| that.marker.setValues({ | |
| position: location, | |
| animation: window.google.maps.Animation.DROP | |
| }); | |
| }, 0); | |
| }); | |
| // Bias the autocomplete object to the user's geographical location, | |
| // as supplied by the browser's 'navigator.geolocation' object. | |
| document.getElementById(addressField).addEventListener('focus', (event) => { | |
| if (navigator.geolocation) { | |
| navigator.geolocation.getCurrentPosition(function(position) { | |
| var geolocation = { | |
| lat: position.coords.latitude, | |
| lng: position.coords.longitude | |
| }; | |
| var circle = new google.maps.Circle( | |
| {center: geolocation, radius: position.coords.accuracy}); | |
| autocomplete.setBounds(circle.getBounds()); | |
| }); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment