Allow user to place marker on a google map

匿名 (未验证) 提交于 2019-12-03 07:50:05

问题:

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>


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!