可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
Is it possible to allow users to do this on the fly? I've seen here and 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?
回答1:
This older 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.
回答2:
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 map
variable 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.
回答3:
<!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>