我正在尝试沿着具有功能的路线移动单个标记。我试图在全局范围内声明标记,但它似乎不起作用......我的其他全局变量确实...
var marker = new google.maps.Marker({map: map});
function playTrip(i) {
if (i < numRows) {
var lat_value = data.getValue(i,3);
var lon_value = data.getValue(i,4);
var loc = new google.maps.LatLng(lat_value, lon_value);
marker.setPosition(loc);
i++;
setTimeout("playTrip("+(i)+")",20);
}
}
答案 0 :(得分:1)
也可以使用全局变量来存储标记和地图。
以下是动态更改地图和标记位置的代码。
<html>
<head>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var marker = null, map = null;
var lon = 14.7730;
function playTrip() {
if (lon < 400){
lon += 1
var loc = new google.maps.LatLng(56.8848, lon);
marker.setPosition(loc);
setTimeout("playTrip()", 3000);
//bounds.extend(loc);
map.setCenter(loc);
}
console.log(lon);
}
(function() {
window.onload = function(){
// Creating a LatLng object containing the coordinate for the center of the map
var latlng = new google.maps.LatLng(56.83, 15.16);
// Creating an object literal containing the properties we want to pass to the map
var options = {
zoom: 7,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
// Calling the constructor, thereby initializing the map
map = new google.maps.Map(document.getElementById('google_map'), options);
marker = new google.maps.Marker({
position: new google.maps.LatLng(56.8848, 14.7730),
map: map,
title: 'My workplace',
clickable: false,
icon: 'http://google-maps-icons.googlecode.com/files/factory.png'
});
playTrip();
}
})();
</script>
<body>
<div id="google_map" style="width:500px;height:400px;"></div>
</body>
</html>
我创建了一个页面here