谷歌地图没有在地图上显示标记

时间:2017-05-14 19:17:42

标签: javascript google-maps marker

我创造了以下js小提琴。任何人都可以看看有什么不对,我看到数据一直传递到markers.push(标记)但没有显示在地图上?

这可能是愚蠢的,但我不知道它可能是什么。

 var locations = [
  ['William T Morrisey Blvd', -42.319081, -71.048592, 6],
  ['William T Morrisey Blvd', -42.319081, -71.048592, 5],
  ['TD Garden', 42.369952, -71.061723, 4],
  ['Terminal C Logan Airport', 42.366906, -71.016455, 3],
  ['Cambridge', 42.373570, -71.110249, 2],
  ['Hardvard', 42.376883, -71.116773, 1]
];

var map;
var markers = []; 

function setMarkers(locations) {



for (i = 0; i < locations.length; i++) {
  marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i][2], locations[i][3]),
    map: map,
    animation: google.maps.Animation.DROP,
    title: 'Hello World!',
  });
    markers.push(marker);


  console.log(locations);

}
}


function initialize() {

   var latlng = new google.maps.LatLng(42.3520576,-71.0726147);


       var myOptions = {
                            zoom: 13,
                            center: latlng,
                            mapTypeId: google.maps.MapTypeId.ROADMAP,
                            disableDefaultUI: false,
                            scrollwheel: true,
                    };



var map = new google.maps.Map(document.getElementById('map-canvas'), myOptions);

    setMarkers(locations);


            }

    initialize();

JS FIDDLE:

http://jsfiddle.net/p646xmcr/498/

1 个答案:

答案 0 :(得分:0)

您已将函数内的map var重新声明为错误的位置

function initialize() {

  var latlng = new google.maps.LatLng(42.3520576, -71.0726147);


  var myOptions = {
    zoom: 13,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    disableDefaultUI: false,
    scrollwheel: true,
  };



 //      var map = new google.maps.Map(document.getElementById('map-canvas'), myOptions);
 //remove ^^ this var declaration 

  map = new google.maps.Map(document.getElementById('map-canvas'), myOptions);

 setMarkers(locations);


}