Javascript 允许用户向谷歌地图添加标记并获取坐标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5757533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:43:55  来源:igfitidea点击:

Allowing users to add markers to google maps and getting the coordinates

javascriptgoogle-mapsgoogle-maps-api-3

提问by tapan

What i want to do is embed a map on my website and allow users to place markers on it (also if there is a way to control how many markers a user can put on the map?) and i also want to get the coordinates of these markers once they have been put on the map. From the documentation that i've read for google maps javascript api V3, i can place markers on the map myself, but i dont see a way to let users put them up on the map. Is there a way to do it?

我想要做的是在我的网站上嵌入地图并允许用户在其上放置标记(如果有办法控制用户可以在地图上放置多少标记?)这些标记一旦放在地图上。从我为 google maps javascript api V3 阅读的文档中,我可以自己在地图上放置标记,但我没有看到让用户将它们放在地图上的方法。有没有办法做到这一点?

回答by tapan

Source: http://code.google.com/apis/maps/documentation/javascript/events.htmlScroll down to accessingarguments in UI events.

来源:http: //code.google.com/apis/maps/documentation/javascript/events.html向下滚动以访问UI 事件中的参数。

var map;
function initialize() {
  var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
  var myOptions = {
    zoom: 4,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

  google.maps.event.addListener(map, 'click', function(event) {
    placeMarker(event.latLng);
  });
}

function placeMarker(location) {
  var marker = new google.maps.Marker({
      position: location, 
      map: map
  });

  map.setCenter(location);
}