谷歌地图,如何禁止拖动标记图标?

时间:2010-06-08 18:05:00

标签: javascript google-maps

我注意到在大多数谷歌地图中,​​您无法将标记图标拖到地址栏中并查看或下载图标.png文件本身。而不是将光标悬停在标记上,您可以看到javascript:void(0)。

这是如何实现的?谢谢!

1 个答案:

答案 0 :(得分:1)

看起来v3 API中的标记无法拖动到地址栏,v2 API中的标记可以。

以下v3示例不允许标记移动(在Firefox和Chrome中测试)。它还会在状态栏中显示javascript:void(0)

<!DOCTYPE html>
<html> 
<head> 
  <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
  <title>Google Maps API No Marker Dragging v3</title> 
  <script src="http://maps.google.com/maps/api/js?sensor=false" 
          type="text/javascript"></script>
</head> 
<body>
  <div id="map" style="width: 500px; height: 400px;"></div>

  <script type="text/javascript">
    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 2,
      center: new google.maps.LatLng(35.00, -25.00),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    new google.maps.Marker({
      position: map.getCenter(),
      map: map
    });

  </script>
</body>
</html>

截图:

Google Maps API No Marker Dragging v3 http://img339.imageshack.us/img339/570/nodrag.jpg

另一方面,使用v2 API的相同示例允许将标记拖动到地址栏:

<!DOCTYPE html>
<html> 
<head> 
  <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
  <title>Google Maps API No Marker Dragging v2</title> 
  <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false" 
          type="text/javascript"></script> 
</head> 
<body onunload="GUnload()">
  <div id="map" style="width: 500px; height: 400px;"></div>

  <script type="text/javascript">
    var map = new GMap2(document.getElementById("map"));
    map.setCenter(new GLatLng(35.00, -25.00), 2);
    map.addOverlay(new GMarker(map.getCenter()));
  </script>
</body>
</html>

截图:

Google Maps API No Marker Dragging v2 http://img39.imageshack.us/img39/8330/yesdrag.jpg