javascript - 将图钉添加到单击的位置

标签 javascript google-maps

我的网站上有以下 Google map ..

           function initialize() {
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map_canvas"));
    map.setCenter(new GLatLng(53.347247,-6.259031), 13);
    map.setUIToDefault();

    GEvent.addListener(map, "click", function(overlay, latLng)
{
    // display the lat/lng in your form's lat/lng fields
    document.getElementById("lat").value = latLng.lat();
    document.getElementById("lng").value = latLng.lng();
});
  }
 }

我需要添加/编辑什么,以便每当用户单击 map 上的某个位置时,图钉/气球/任何类型的指示器都会掉落在他们单击的位置?

谢谢。

最佳答案

您所要做的就是将其添加到现有的 addListener 调用中:

if (latLng) {
    marker = new GMarker(latLng, {draggable:true});
    map.addOverlay(marker);
}

参见this article Google 在其 API 信息中链接了一个更高级的示例,可让您的用户编辑 map 数据。

更新:将 latLng 中“l”的大小写更改为“L”。

关于javascript - 将图钉添加到单击的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3291458/

相关文章:

javascript - 谷歌地图无法在 Chrome 上运行

android - 谷歌地图 toScreenLocation() 方法 Android 的问题

javascript - 谷歌街景 : Marker size and distance

java - Struts2 - 在 JSP 迭代期间调用 Javascript 函数

javascript - 网站如何向控制台输出html评论

javascript - 如何在 Three.js 中用多个 '.obj' 文件加载 '.mtl'

javascript - 添加到列表时会触发 Knockout JS 单击事件

android - 如何通过来自 google maps api 的流量获取导航持续时间?

javascript - 使用过滤器删除数组中的对象会删除整个数组

javascript - 开 Jest , enzyme - 测试返回 jsx 的方法