且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Google Maps API - 地图不显示 - 没有错误

更新时间:2023-08-30 09:41:34

问题是:


  • 我没有使用API​​密钥(尽管它不认为这是恶意的,但它工作)

  • 我没有设置我的div的宽度/高度。

  • 我忘记了这样做:

     初始化(); 

    函数initialize(){
    google.maps.event.addDomListener(window,'load',function(){
    codeAddress('London');
    });
    }




谢谢!


I am trying to load a map from the Google API into a div. However, the map is not loaded, and no errors are outputted. This is the code:

// google maps

var geocoder, map;
function codeAddress(address) {
    geocoder = new google.maps.Geocoder();
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        var myOptions = {
        zoom: 8,
        center: results[0].geometry.location,
        mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);

        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });
      }
    });
  }


  google.maps.event.addDomListener(window, 'load', function() {
    codeAddress('" . $location['address'] . " " . $location['zip'] . " " . $location['city'] . " " . $countries[$location['country']] . "');
  });

I have been dealing with the problem for quite a long time now, and I am out of ideas.

Anybody of you familiar with the google maps api, who knows what's wrong with my code? My div does exist with the ID: map_canvas.

The problem was:

  • I didn't use API-key (even though it don't think this was nessecary, but it worked)
  • I didn't set the width/height of my div.
  • I forgot to initalize like so:

    initialize();
    
    function initialize() {
      google.maps.event.addDomListener(window, 'load', function() {
        codeAddress('London');
    });
    }
    

It now works. Thanks!