function initMap() {
const myLatlng = {
lat: 21.7645,
lng: 72.1519
};
const map = new google.maps.Map(document.getElementById("map"), {
zoom: 10,
center: myLatlng,
});
let infoWindow = new google.maps.InfoWindow({
content: "Click the map to get Lat/Lng!",
position: myLatlng,
});
infoWindow.open(map);
const marker = new google.maps.Marker({
position: myLatlng,
map,
title: "Click to zoom",
});
map.addListener("center_changed", () => {
window.setTimeout(() => {
map.panTo(marker.getPosition());
}, 3000);
});
map.addListener("click", (mapsMouseEvent) => {
infoWindow.close();
infoWindow = new google.maps.InfoWindow({
position: mapsMouseEvent.latLng,
});
infoWindow.setContent(
JSON.stringify(mapsMouseEvent.latLng.toJSON(), null, 2)
);
infoWindow.open(map);
const data = JSON.stringify(mapsMouseEvent.latLng.toJSON(), null, 2);
const obj = JSON.parse(data);
console.log(obj);
document.getElementById("latitude").value = obj['lat'];
document.getElementById("longitude").value = obj['lng'];
});
}