I'm not able to initialize my map using javascript.
Basically I created 2 files to test the creation, a html
file with a div
to contain the map and a js
file with the map creation function. But whenever I try to call the map creation function, an error occurs saying that the variable "google" is undefined. Here is the code for the 2 files.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="map"></div>
<script src="js/Mapa.js"></script>
<script src="http://maps.googleapis.com/maps/api/js?key=MINHAKEY&sensor=false"></script>
</body>
</html>
Map.js
function initMap() {
var uluru = {lat: -25.363, lng: 131.044};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: uluru
});
var marker = new google.maps.Marker({
position: uluru,
map: map
});
}
initMap();
The problem here is this, when I put the initMap()
function as a direct script in the html
file, the map starts and works, but when I put the initMap()
in the file Mapa.js
and import the js
file simply does not work. Can anyone help me with this problem? Thank you in advance for your cooperation!