如何在 Google 地图 API 中移动标记
我正在使用 Google Maps API V3,并且正在尝试使标记在屏幕上移动。这就是我所拥有的:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map_canvas { height: 100% }
</style>
<script type="text/javascript"
src="http://maps.googleapis.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">
function initialize() {
var myLatLng = new google.maps.LatLng(50,50);
var myOptions = {
zoom: 4,
center: myLatLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
image = 'bus.gif';
marker = new google.maps.Marker({position: myLatLng, map: map, icon: image});
marker.setMap(map);
}
function moveBus()
{
// Move Bus
}
</script>
</head>
<body onload="initialize()">
<script type="text/javascript">
moveBus();
</script>
<div id="map_canvas" style="height: 500px; width: 500px;"></div>
</body>
</html>
现在我尝试的是将 // Move Bus 替换为
marker.setPosition(new google.maps.LatLng(0,0));
但那没有做任何事情。这是我在 API 参考中看到的命令。我对 Javascript 也比较陌生,所以这可能是我的 JS 错误。
I'm using the Google Maps API V3 and I'm trying to make a marker move across the screen. Here's what I have:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map_canvas { height: 100% }
</style>
<script type="text/javascript"
src="http://maps.googleapis.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">
function initialize() {
var myLatLng = new google.maps.LatLng(50,50);
var myOptions = {
zoom: 4,
center: myLatLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
image = 'bus.gif';
marker = new google.maps.Marker({position: myLatLng, map: map, icon: image});
marker.setMap(map);
}
function moveBus()
{
// Move Bus
}
</script>
</head>
<body onload="initialize()">
<script type="text/javascript">
moveBus();
</script>
<div id="map_canvas" style="height: 500px; width: 500px;"></div>
</body>
</html>
Now what I've tried is replacing // Move Bus with
marker.setPosition(new google.maps.LatLng(0,0));
But that didn't do anything. That's the command I saw on the API reference. I'm also relatively new to Javascript, so it might be a JS error on my part.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
moveBus()
在initialize()
之前被调用。尝试将该行放在initialize()
函数的末尾。另外,纬度/经度 0,0 不在地图上(它是坐标,而不是像素),因此当它移动时您看不到它。尝试 54,54。如果您希望地图中心移动到新位置,请使用panTo()
。演示:http://jsfiddle.net/ThinkingStiff/Rsp22/
HTML:
CSS:
脚本:
moveBus()
is getting called beforeinitialize()
. Try putting that line at the end of yourinitialize()
function. Also Lat/Lon 0,0 is off the map (it's coordinates, not pixels), so you can't see it when it moves. Try 54,54. If you want the center of the map to move to the new location, usepanTo()
.Demo: http://jsfiddle.net/ThinkingStiff/Rsp22/
HTML:
CSS:
Script:
只需尝试创建标记并将可拖动属性设置为
true
。代码如下:
我希望这有帮助!
Just try to create the marker and set the draggable property to
true
.The code will be something as follows:
I hope this helps!
您使用的是正确的 API,但“标记”变量对整个脚本可见。我没有看到这个全局声明的标记变量。
You are using the correct API, but is the "marker" variable visible to the entire script. I don't see this marker variable declared globally.
这是完整的代码,没有错误
Here is the full code with no errors
使用 panTo(x,y)。这会对你有帮助
use panTo(x,y).This will help u