Google Maps API - 如何每2秒更新和刷新一次标记?

时间:2016-01-23 05:35:04

标签: javascript php json google-maps-api-3

我有这个'实时地图'在我的游戏服务器上在线显示玩家。我添加了一个2秒计时器,它可以更新标记位置,但它根本不起作用。我做错了什么?

JavaScript:

<div id="map-canvas"></div>
var p_pos = <?php echo (empty($json_pos)) ? "" : $json_pos ?>;
var mapType = new SanMapType(0, 1, function (zoom, x, y) {
    return x == -1 && y == -1 
    ? "images/tiles/map.outer.png" 
    : "images/tiles/map." + zoom + "." + x + "." + y + ".png";//Where the tiles are located
});

var satType = new SanMapType(0, 3, function (zoom, x, y) {
    return x == -1 && y == -1 
    ? null 
    : "images/tiles/sat." + zoom + "." + x + "." + y + ".png";//Where the tiles are located
});

var map = SanMap.createMap(document.getElementById('map-canvas'), 
    {'Map': mapType, 'Satellite': satType}, 2, SanMap.getLatLngFromPos(0,0), false, 'Satellite');
map.controls[google.maps.ControlPosition.TOP_RIGHT].push(document.getElementById('map-legend'));
if(p_pos !== "")
{
    for (var i = 0; i < Object.keys(p_pos).length; i++) 
    {
        console.log(p_pos[i].online);
        if(p_pos[i].online == 1) createMarker(i); 
    }
}   
google.maps.event.addListener(map, 'click', function(event) {
    var pos = SanMap.getPosFromLatLng(event.latLng);
    console.log(pos.x + "," + pos.y);
});                         
function createMarker(id)
{
    var p_windows = new google.maps.InfoWindow({
        content: "<p>"+p_pos[id].name+" <b>(ID: "+id+")</b><br>Ping: "+p_pos[id].ping+"<br>X: "+p_pos[id].x+"<br>Y: "+p_pos[id].y+"</p>"
    });
    var p_marker = new google.maps.Marker({
        position: SanMap.getLatLngFromPos(p_pos[id].x, p_pos[id].y),
        map: map,
        icon: "https://wiki.sa-mp.com/wroot/images2/8/81/Icon_0.gif"
    });
    google.maps.event.addListener(p_marker, 'click', function() {
        p_windows.open(map,p_marker);
    });
}

计时器:

<script>
$(function () {
    setTimeout(function () { $('#map-canvas').load(initialize); }, 1000);
})
</script>

0 个答案:

没有答案