2014-02-07 175 views
0

我使用Javascript API创建地图,并且在显示标记时遇到了一些麻烦。将标记添加到Google地图

我已经按照本教程创建地图,效果很好:

https://developers.google.com/maps/tutorials/fundamentals/adding-a-google-map

我再接着本教程中添加的标记,但它不加载:

https://developers.google.com/maps/documentation/javascript/examples/marker-simple

这里是我的代码现在:

  <script> 
     function initialize() { 
      var map_canvas = document.getElementById('map_canvas'); 
      var map_options = { 
      center: new google.maps.LatLng(43.643296, -79.408475), 
      zoom: 15, 
      mapTypeId: google.maps.MapTypeId.ROADMAP } 
      var map = new google.maps.Map(map_canvas, map_options, marker); 
      var marker = new google.maps.Marker({ 
      position: myLatlng, 
      map: map, 
      title:"Hello World!" }); 
      } 

      google.maps.event.addDomListener(window, 'load', initialize); 

      </script> 

回答

0

此行

var map = new google.maps.Map(map_canvas, map_options, marker); 

是错误的。 map构造函数只有两个参数。它应该是

var map = new google.maps.Map(map_canvas, map_options); 

myLatlng没有定义。因此,您可以将您的代码更改为:

function initialize() { 
    myLatlng = new google.maps.LatLng(43.643296, -79.408475); 

    var map_canvas = document.getElementById('map'); 
    var map_options = { 
     center: myLatlng, 
     zoom: 15, 
    mapTypeId: google.maps.MapTypeId.ROADMAP } 
    var map = new google.maps.Map(map_canvas, map_options); 

    var marker = new google.maps.Marker({ 
     position: myLatlng, 
     map: map, 
     title:"Hello World!" }); 
} 
相关问题