0

I get the ReferenceError google is not defined.

I searched for an solution, but nothing helped me.

Any ideas? Thx

Edit: relevant Code

in index.html:

    <script src="dojo/dojo-release-1.12.2/dojo/dojo.js"></script><!--Dojo Script-->
    <script src="js/index.js"></script>

    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/index.css">
</head>
<!--Google Maps API-->
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=APICode&callback=initMap" async defer></script>
    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script> 
</body>

in index.js relevant function which do something with google maps:

function setMapOnAll(map) {
    for (i = 0; i < markers.length; i++) {
        markers[i].setMap(map);
    }
}

function RefreshMap() {
    location.reload();
    initMap();
}

function initMap() {
var loggedin = localStorage.getItem("loggedIn");
if(loggedin == 0) {
    var place = {lat: 52.558643, lng: 13.351725};
    map = new google.maps.Map(document.getElementById('map'), {
        zoom: 3,
        center: place
    });
}

else {
    map = new google.maps.Map(document.getElementById('map'), {
        //zoom: 3,
        //center: place
    });

    GetLocations();

    var geocoder = new google.maps.Geocoder();
    document.getElementById("btnAdd").addEventListener("click", function() {
        geocodeAddress(geocoder, map);
    });
}
}

function GetLocationsDojo() {
require(["dojo/store/JsonRest"], function(JsonRest) {               
    var username = localStorage.getItem("username");
    var session = localStorage.getItem("sessionID");

    var store = new JsonRest ({
        target: "http://localhost:8080/FAPServer/service/fapservice/getStandort",
        headers: {
            "Content-Type": "application/json"
        }
    });

    locations = [];
    for(i = 0; i < users.length; i++) {
        store.get("?login=" + username + "&session=" + session + "&id=" + users[i]).then(
        function(data) {
            //console.log(data);    

            var breite = data.standort.breitengrad;
            var laenge = data.standort.laengengrad;

            var marker = new google.maps.Marker({
                position: new google.maps.LatLng(breite, laenge),
                map: map
            });

            infowindow = new google.maps.InfoWindow();
            google.maps.event.addListener(marker, 'click', (function(marker, i) {
                return function() {
                    infowindow.setContent(data.loginName);
                    infowindow.open(map, marker);
                }
            })(marker, i));

            markers.push(marker);   

            //bounds map
            var bounds = new google.maps.LatLngBounds();
            for (var i = 0; i < markers.length; i++) {
                bounds.extend(markers[i].getPosition());
            }
            map.fitBounds(bounds);              
        },
        function(data) {}
        );
    }
});         
} 
moxmlb
  • 1,190
  • 3
  • 10
  • 18
  • Where/when do you call RefreshMap? – duncan May 17 '17 at 11:11
  • If i press a button at the website – moxmlb May 17 '17 at 11:13
  • 1
    Where do you create your marker; it's not in your initMap function? What do the values for data.standort.breitengrad and data.standort.laengengrad look like? – duncan May 17 '17 at 14:20
  • I create the marker in an function which read the locations from a server. vreitengrad and laengengrad are double values – moxmlb May 17 '17 at 14:42
  • 1
    Well it sounds like that function's getting called before the google maps JS is loaded. – duncan May 17 '17 at 15:10
  • And how I can fix that? That the google maps js is loaded before the function is called? – moxmlb May 18 '17 at 06:31
  • 2
    It'd help if you put all the relevant code into your question – duncan May 18 '17 at 07:27
  • I have add the code. but if you need more i would like to send you that per mail or something else – moxmlb May 18 '17 at 08:46
  • Possible duplicate of [How do I include a JavaScript file in another JavaScript file?](https://stackoverflow.com/questions/950087/how-do-i-include-a-javascript-file-in-another-javascript-file) – moxmlb Sep 26 '17 at 10:51

1 Answers1

1

async defer delays execution of the script. It probably is running (and adding the variable you are trying to access) after your index.js script.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335