簡體   English   中英

地圖標記未顯示(JavaScript / Google Maps API V3)

[英]Map Markers Not Displaying (JavaScript/Google Maps API V3)

我無法使用Google Maps API v3顯示地圖標記。 我試圖將它們全部保存在一個陣列中,以顯示相對簡單的大量數據。 目前,地圖加載正常,但拋出錯誤Uncaught TypeError: Object #<Object> has no method 'setValues'在嘗試繪制標記時Uncaught TypeError: Object #<Object> has no method 'setValues' 每次迭代都會由setTimeout()重復該錯誤。 任何建議將不勝感激。

這是使用的JavaScript:

var map;
var markers = [
    [
        45.768366,
        -108.5975760,
        'Fitness 19'
    ],
    [
        45.785684,
        -108.6144625,
        'Granite Fitness'
    ],
      ... (more, syntactically correct)
    [
        45.7920092,
        -108.4886232,
        'Steepworld'
    ]
];
function mapinit() {
    var conf = {
        zoom: 11,
        center: new google.maps.LatLng(45.7832856,-108.5006904),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById('mapcont'),conf);
    for(i in markers) {
        window.setTimeout('mark('+i+')',i*200);
    }
}
function mark(i){
    google.maps.Marker({
        position: google.maps.LatLng(markers[i][0],markers[i][1]),
        animation: google.maps.Animation.DROP,
        title: markers[i][2]
    });
}

這是new關鍵字,讓一切變得與眾不同!

我有同樣的問題。 在構建Marker對象時使用new關鍵字使其至少在Chrome中再次運行。 把它放在一個不同的超時事件中沒有。 在這里,我認為new只不過是語法糖......

好的,在Chrome的JavaScript控制台(我喜歡那個東西)中亂七八糟之后,我能夠讓它完美運行。 我將mapinit()mark()函數重寫為:

function mapinit() {
    var conf = {
        zoom: 11,
        center: new google.maps.LatLng(45.7832856,-108.5006904),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById('mapcont'),conf);
    for(i in markers) {
        markers[i][3] = new google.maps.LatLng(markers[i][0],markers[i][1]);
        window.setTimeout('mark('+i+')',i*200);
    }
}
function mark(i){
    new google.maps.Marker({
        position: markers[i][3],
        animation: google.maps.Animation.DROP,
        map: map,
        title: markers[i][2]
    });
}

這里的主要區別是標記的position變量似乎需要由於某種原因在外部變量中初始化,所以當我循環遍歷markers數組時,我生成google.maps.LatLng作為每個標記的第四項。 然后在mark()函數中引用它,並且標記成功顯示。 用於錯開標記顯示的setTimeout非常有效,特別是在腳本和地圖快速加載的快速連接上。

在我的inClass網站上查看最終結果

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM