JSFiddle - React, Tailwind, and code Playground
HTML
<!DOCTYPE html>
<html>
<head>
<title>Info Windows</title>
<script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
<!-- jsFiddle will insert css and js -->
</head>
<body>
<div id="map"></div>
<!-- Async script executes immediately and must be after any DOM elements used in callback. -->
<script
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&callback=initMap&v=weekly&channel=2"
async
></script>
</body>
</html>
CSS
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
JavaScript
let locations = [
["location 1", -25.363, 131.044,'A good location.'],
["location 2", -20, 140, 'Location two is hot.']
];
// This example displays a marker at the center of Australia.
// When the user clicks the marker, an info window opens.
function initMap() {
const uluru = { lat: -25.363, lng: 131.044 };
const map = new google.maps.Map(document.getElementById("map"), {
zoom: 4,
center: uluru,
});
const infowindow = new google.maps.InfoWindow({
content: 'abc',
});
for (let i = 0; i < locations.length; i++) {
let position = new google.maps.LatLng(locations[i][1], locations[i][2]);
let marker = new google.maps.Marker({
position: position,
map: map,
title: locations[i][0]
});
google.maps.event.addListener(marker, 'click', function (event) {
console.log(event);
let content = `<b>${locations[i][0]} </b><br>
${locations[i][3]}<br>
Position: ${event.latLng.lat()}, ${event.latLng.lng()}`;
infowindow.setContent(content);
infowindow.setOptions({ maxWidth: 200 });
infowindow.open(map, marker);
});
}
}