googlemaps / googlemaps/js-samples
Bug: event-click-latlng
- Dominant language
- TypeScript
- Stars
- 805
- Forks
- 836
- PR merge metrics
- No merged PRs in 30d
Description
### Operating system
ubuntu 22.04
### Browser Version
Chrome 104.0.5112.101 (oficial) 64 bits
### How severe is the bug?
moderate
### Bug description
function initMap() {
const myLatlng = { lat: -25.363, lng: 131.044 };
const map = new google.maps.Map(document.getElementById("map")!, {
zoom: 4,
center: myLatlng,
});
// Create the initial InfoWindow.
let infoWindow = new google.maps.InfoWindow({
content: "Click the map to get Lat/Lng!",
position: myLatlng,
});
infoWindow.open(map);
// Configure the click listener.
map.addListener("click", (mapsMouseEvent) => {
// Close the current InfoWindow.
infoWindow.close();
// Create a new InfoWindow.
infoWindow = new google.maps.InfoWindow({
position: mapsMouseEvent.latLng,
});
infoWindow.setContent(
JSON.stringify(mapsMouseEvent.latLng.toJSON(), null, 2)
);
infoWindow.open(map);
});
}
declare global {
interface Window {
initMap: () => void;
}
}
window.initMap = initMap;
export {};
### Steps to reproduce
Just run.
### Console log output
```sh
I fixed it by commenting some parts below
function initMap() {
const myLatlng = { lat: -25.363, lng: 131.044 };
const map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: myLatlng,
});
// Create the initial InfoWindow.
let infoWindow = new google.maps.InfoWindow({
content: "Click the map to get Lat/Lng!",
position: myLatlng,
});
infoWindow.open(map);
// Configure the click listener.
map.addListener("click", (mapsMouseEvent) => {
// Close the current InfoWindow.
infoWindow.close();
// Create a new InfoWindow.
infoWindow = new google.maps.InfoWindow({
position: mapsMouseEvent.latLng,
});
infoWindow.setContent(
JSON.stringify(mapsMouseEvent.latLng.toJSON(), null, 2)
);
infoWindow.open(map);
});
}
/*
declare global {
interface Window {
initMap: () => void;
}
}*/
window.initMap = initMap;
//export {};
```
Contributor guide
Assessment
This issue has not been assessed yet.