I am using the example from this document as reference. https://developers.google.com/maps/documentation/javascript/examples/layer-traffic
Basically, I have a html file and an external js file.
<!DOCTYPE html>
<html>
<head>
<title>Transit Layer</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>
<script
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&callback=initMap(51.501904,-0.115871)&v=weekly"
></script>
</body>
</html>
External js script:
function initMap(lat, lon) {
const map = new google.maps.Map(document.getElementById("map"), {
zoom: 13,
center: { lat: lat, lng: lon },
});
const transitLayer = new google.maps.TransitLayer();
transitLayer.setMap(map);
}
window.initMap = initMap;
How do I pass arguments initMap(51.501904,-0.115871) to the js function from html? I tried this in jsfiddle and it didn't work.
latandlong.<button type="button" onclick="initMap(51.501904,-0.115871)">Click to initialize map</button>