javascript 允许用户在谷歌地图上放置标记

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7916438/
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-10-26 01:41:49  来源:igfitidea点击:

Allow user to place marker on a google map

javascriptgoogle-maps

提问by LiamB

I'm looking for a way to allow a user to place a marker on a Google map and then get the co-ordinates so I can email them to a user.

我正在寻找一种方法,允许用户在 Google 地图上放置标记,然后获取坐标,以便我可以通过电子邮件将它们发送给用户。

Is it possible to allow users to do this on the fly? I've seen hereand understand I can do this using longitude and Latitude. So using the above method can I get a Longitude and Latitude from a users click?

是否可以允许用户即时执行此操作?我在这里看到并明白我可以使用经度和纬度来做到这一点。那么使用上述方法可以从用户点击中获得经度和纬度吗?

采纳答案by Tin Can

Thisolder post is what you're looking for I think. Add in an ajax call with the lat/long and you should be good to go.

我认为这篇较旧的帖子正是您要查找的内容。添加一个带有纬度/经度的 ajax 调用,你应该很高兴。

回答by Stefan Kovachev

You can achieve this using the following code:

您可以使用以下代码实现此目的:

google.maps.event.addListener(map, 'click', function( event ){
  alert( "Latitude: "+event.latLng.lat()+" "+", longitude: "+event.latLng.lng() ); 
});

The mapvariable is your google.maps.Map instance. Every time the user clicks the map an alert box will display the coordinates of the place he clicked.

map变量是您google.maps.Map实例。每次用户点击地图时,都会有一个警告框显示他点击的地点的坐标。

回答by m-tech

<!DOCTYPE html>
<html>
<body>
<div id="googleMap" style="width:100%;height:400px;"></div>

<script>
function myMap() {
var mapProp= {
    center:new google.maps.LatLng(51.508742,-0.120850),
    zoom:5,
};
var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);

google.maps.event.addListener(map, 'click', function(event) {
alert(event.latLng.lat() + ", " + event.latLng.lng());
});

}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBu-916DdpKAjTmJNIgngS6HL_kDIKU0aU&callback=myMap"></script>
</body>
</html>