Problem creating map using API Google Maps Javascript

0

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&amp;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!

    
asked by anonymous 27.07.2017 / 23:15

1 answer

0

From what I understand, it may be the order that is putting the files, try to invert the upload as follows:

                      Title          

    <div id="map"></div>

    <script src="http://maps.googleapis.com/maps/api/js?key=MINHAKEY&amp;sensor=false"></script><scriptsrc="js/Mapa.js"></script>


</body>

    
28.07.2017 / 02:09