Skip to content

Instantly share code, notes, and snippets.

@learyjk
Last active March 29, 2023 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save learyjk/a55e9cc748c757ae8a5238791dc5878f to your computer and use it in GitHub Desktop.
Save learyjk/a55e9cc748c757ae8a5238791dc5878f to your computer and use it in GitHub Desktop.
function initialize() {
var markers = [];
var input = /** @type {HTMLInputElement} */
(document.getElementById('Address'));
var options = {
componentRestrictions: {
country: 'gb'
}
}
var autocomplete = new google.maps.places.Autocomplete(input,options);
var map = new google.maps.Map(document.getElementById('map'),{
mapTypeId: google.maps.MapTypeId.SATELLITE,
disableDefaultUI: true,
tilt: 0,
});
google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place = autocomplete.getPlace();
if (!place) {
return;
}
var bounds = new google.maps.LatLngBounds();
var image = {
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)
};
var marker = new google.maps.Marker({
draggable: true,
map: map,
icon: image,
title: place.name,
position: place.geometry.location
});
markers.push(marker);
bounds.extend(place.geometry.location);
map.fitBounds(bounds);
document.getElementById('Lat').value = marker.getPosition().lat();
document.getElementById('Lng').value = marker.getPosition().lng();
// Add a listener to the marker for the dragend event
google.maps.event.addListener(marker, 'dragend', function() {
// Get the updated position of the marker
var newPosition = marker.getPosition();
// Update the latitude and longitude fields with the new coordinates
document.getElementById('Lat').value = newPosition.lat();
document.getElementById('Lng').value = newPosition.lng();
});
});
google.maps.event.addListener(map, 'bounds_changed', function() {
var bounds = map.getBounds();
autocomplete.setBounds(bounds);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment