2017-05-14 159 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); 


}