JSFiddle - React, Tailwind, and code Playground
HTML
<!DOCTYPE html>
<html>
<head>
<title>Remove Markers</title>
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
var map;
var markers = [];
function initMap() {
var haightAshbury = {lat: 50.43389186200793, lng: 30.532379150390625};
map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: haightAshbury,
mapTypeId: google.maps.MapTypeId.TERRAIN
});
// This event listener will call addMarker() when the map is clicked.
map.addListener('click', function(event) {
deleteMarkers();
addMarker(event.latLng);
});
// Adds a marker at the center of the map.
//addMarker(haightAshbury);
}
// Adds a marker to the map and push to the array.
function addMarker(location) {
var lat, lng;
var marker = new google.maps.Marker({
position: location,
map: map
});
lng=marker.getPosition().lng();
lat=marker.getPosition().lat();
alert('Координаты: ' + lat + ' - ' + lng)
markers.push(marker);
}
// Sets the map on all markers in the array.
function setMapOnAll(map) {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(map);
}
}
// Removes the markers from the map, but keeps them in the array.
function clearMarkers() {
setMapOnAll(null);
}
// Deletes all markers in the array by removing references to them.
function deleteMarkers() {
clearMarkers();
markers = [];
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCvDO-bzko8kfMYP9uhI81n4AiejPIthvU&signed_in=true&callback=initMap"></script>
</body>
</html>