iGenezys's avatar

Google map - Not loading when refresh page

Hi people.

I wanted to try to google map API in an application that :

  • Create an User (Email, adress, city, postal code),
  • Define the latitude & longitude from the adress,
  • Put a mark on google map with this adress.

Actually, it's working well, you can even try on my site : mapuser.leroy-thomas.fr

But I found a problem.

Sometimes, when you reload the page, the map don't want to appear, and in the console, I got an error :

Uncaught 
Qb {message: "initMap is not a function", name: "InvalidValueError", stack: "Error↵    at new Qb (https://maps.googleapis.com/m…KHdBCcpmkMnyrgIbpupNBQL5c&callback=initMap:150:59"}
message:"initMap is not a function"
name:"InvalidValueError"

I don't know why it's doing that, because it's a bit random, sometimes I can reload the page 10 times without any erros, and after the page don't want to reload.

If you want my JS code :

<script type="text/javascript">
        var map;

        function initMap() {
            map = new google.maps.Map(document.getElementById('map'), {
                center: {lat: 48.764651, lng: 0.628715},
                zoom: 8
            });
        }

        function addMarker(location) {
            marker = new google.maps.Marker({
                position: location,
                map: map
            });
        }

        function marker(){
            City = new google.maps.LatLng(48.764651, 0.628715);
            addMarker(City);
        }

        $.ajax({
            url: "/mapuser/getUser",
            type: "GET",
            dataType: "json",
            success: function( response ){

                $.each( response, function( index, value ){
                    City = new google.maps.LatLng(value.latitude, value.longitude);
                    addMarker(City);
                });
            }
        });
    </script>

Well, you can try on the website, and if you have an idea about why it's doing that, and what to do for solve this problem, I thank you in advance.

0 likes
2 replies
topvillas's avatar
Level 46

Load the google map api script after your inline script. It's sometimes managing to load before the inline script and is unable to find the callback.

Or even better, put that inline script in a js file and load it before the google map api.

Please or to participate in this conversation.