简体   繁体   English

地图标记未显示(JavaScript / Google Maps API V3)

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

I'm having trouble getting map markers to display using Google Maps API v3. 我无法使用Google Maps API v3显示地图标记。 I'm attempting to keep them all in a single array, to make displaying a large amount relatively simple. 我试图将它们全部保存在一个阵列中,以显示相对简单的大量数据。 Currently, the map loads fine, but throws the error Uncaught TypeError: Object #<Object> has no method 'setValues' when attempting to draw the markers. 目前,地图加载正常,但抛出错误Uncaught TypeError: Object #<Object> has no method 'setValues'在尝试绘制标记时Uncaught TypeError: Object #<Object> has no method 'setValues' The error repeats with each iteration run by the setTimeout() . 每次迭代都会由setTimeout()重复该错误。 Any recommendations will be greatly appreciated. 任何建议将不胜感激。

This is the JavaScript used: 这是使用的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]
    });
}

It's the new keyword that makes all the difference! 这是new关键字,让一切变得与众不同!

I had the same problem. 我有同样的问题。 Using the new keyword when constructing the Marker object makes it work again in Chrome at least. 在构建Marker对象时使用new关键字使其至少在Chrome中再次运行。 Putting it in a differed timeout event didn't. 把它放在一个不同的超时事件中没有。 And here I thought new was nothing but syntactic sugar... 在这里,我认为new只不过是语法糖......

Okay, after quite a bit of messing around in Chrome's JavaScript console (I love that thing), I was able to get it working perfectly. 好的,在Chrome的JavaScript控制台(我喜欢那个东西)中乱七八糟之后,我能够让它完美运行。 I rewrote the mapinit() and mark() functions to this: 我将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]
    });
}

The main difference here is that the position variable of the marker seems to require being initialized in an outside variable for some reason, so when I loop through the markers array, I generate a google.maps.LatLng as the fourth item for each marker. 这里的主要区别是标记的position变量似乎需要由于某种原因在外部变量中初始化,所以当我循环遍历markers数组时,我生成google.maps.LatLng作为每个标记的第四项。 This is then referenced within the mark() function, and the marker displays successfully. 然后在mark()函数中引用它,并且标记成功显示。 The setTimeout for staggering the display of the markers works wonderfully, especially on faster connections where the scripts and map load quickly. 用于错开标记显示的setTimeout非常有效,特别是在脚本和地图快速加载的快速连接上。

View the end result on my inClass site 在我的inClass网站上查看最终结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM