Google map error: a is null

后端 未结 10 1885
予麋鹿
予麋鹿 2021-01-03 17:15

I have a program that I want to use google maps for. The problem is I get an error that says a is null where a is a var used in the google map api. Here is how I call my goo

10条回答
  •  长情又很酷
    2021-01-03 18:03

    Had the exact same problem and this is have i fixed it for me.

    The thing was that I had 2 google maps in my website - one in the footer and the other one on the contact page, but i called them both in one JS file like so:

    var map1 = new google.maps.Map(document.getElementById("map-canvas-footer"), settings1);
    var map2 = new google.maps.Map(document.getElementById("map-canvas"), settings2);
    

    But the thing is that the object with id="map-canvas" was located only on the contact page. So at first you have to check if that element exists on the page like so:

    if ($("#map-canvas-footer").length > 0){
        var map1 = new google.maps.Map(document.getElementById("map-canvas-footer"), settings1);        
    }
    
    if ($("#map-canvas").length > 0){
        var map2 = new google.maps.Map(document.getElementById("map-canvas"), settings2);
    }
    

    I hope this can help someone else as well ;)

提交回复
热议问题