【发布时间】:2014-10-09 15:41:38
【问题描述】:
我在地图上放置了一个可拖动属性为 true 的标记。我想限制移动标记距离原始位置不超过 500 米。我该怎么做?有没有更好的方法我试过了?
我尝试了拖动事件。我用代码 I found on SO 测量距离。但我不知道如何防止阻力。我尝试返回false,尝试查看函数参数是否有取消或类似的东西。
google.maps.event.addListener(marker, 'drag', function(e)
{
var distance = Application.getMapDistance(data[0].geometry.location, marker.getPosition());
document.title = distance;
if (distance > 500)
return false; //not working, e has no cancel or something like that
});
编辑:
显然有人对我的问题不满意。是的,基本逻辑是将谷歌标记位置设置为持续良好,但是这样,当它移动到边界之外时,标记会闪烁。 我很确定 Google 可以做得更好。
google.maps.event.addListener(marker, 'drag', function(e)
{
var distance = Application.getMapDistance(data[0].geometry.location, marker.getPosition());
document.title = distance;
if (distance > 500)
marker.setPosition(lastGoodPosition);
else
lastGoodPosition = marker.getPosition();
});
【问题讨论】:
-
您是否尝试过在标记被拖出界限时重置标记位置?
-
不,我虽然有更好的方法。通过将标记位置设置为新坐标,我预计会出现一些闪烁。
-
不自己开发标记类和拖拽实现,想不出更好的办法。
标签: google-maps google-maps-api-3