2017-05-14 118 views
1

我已经创建了以下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/

+0

你没有初始化全局'map'变量,无论是在初始化时从'map'之前删除'var'或将'map'变量传递给'setMarkers'函数。 – geocodezip

+0

[updated fiddle](http://jsfiddle.net/geocodezip/p646xmcr/499/)并且您使用的纬度/经度错误索引(您正在使用2&3,应该是1&2) – geocodezip

+0

谢谢我知道这是一件小事 – MaximusM

回答

0

你重新声明地图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); 


}