javascript 在没有页面加载的情况下更新谷歌地图上的标记位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19957617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 17:21:28  来源:igfitidea点击:

Update marker position on google map without page load

javascriptgoogle-mapsgoogle-maps-api-3

提问by Alex

I'm trying to update the google map marker position in every 20 seconds which comes from database but it's doesn't work.

我试图每 20 秒更新一次来自数据库的谷歌地图标记位置,但它不起作用。

Code :

代码 :

for (i = 0; i < purple.length; i++) {

    if (purple[i][1] == latitude && purple[i][2] == longitude) {
        nogreen = 1;
    }

    marker = new google.maps.Marker({
            position : new google.maps.LatLng(purple[i][1], purple[i][2]),
            map : map,
            title : purple[i][0],
            data : purple[i][0],
            zoom : maxZoomService,
            icon : 'img/purple.png',
            shadow : 'img/purple.png'
        });

    setInterval(function () {
        position : new google.maps.LatLng(purple[i][1], purple[i][2]),
        marker.setPosition(position);
    }, 20000);
};

Is this correct or how can i do that ?

这是正确的还是我该怎么做?

回答by duncan

This code here isn't valid javascript:

这里的代码不是有效的 javascript:

setInterval(function() {
        position: new google.maps.LatLng(purple[i][1], purple[i][2]),
        marker.setPosition(position);
    }, 20000); 

You want to do:

你想做:

setInterval(function() {
        position = new google.maps.LatLng(purple[i][1], purple[i][2]);
        marker.setPosition(position);
    }, 20000);