1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <title>Simple Map</title>
    5. <meta name="viewport" content="initial-scale=1.0">
    6. <meta charset="utf-8">
    7. <style>
    8. html,
    9. body {
    10. height: 100%;
    11. margin: 0;
    12. padding: 0;
    13. }
    14. #map {
    15. height: 100%;
    16. }
    17. </style>
    18. </head>
    19. <body>
    20. <div id="map"></div>
    21. <script>
    22. function initMap() {
    23. var myLatlng = {
    24. lat: 39.921323,
    25. lng: 116.426239
    26. };
    27. var marker ;
    28. var markersArray = [];
    29. var map = new google.maps.Map(document.getElementById('map'), {
    30. zoom: 12,
    31. center: myLatlng
    32. });
    33. map.addListener('click', function(e) {
    34. addMarker(e.latLng, map);
    35. //根据经纬度获取 当前地理信息
    36. var latLngData = e.latLng.lat().toFixed(6)+','+e.latLng.lng().toFixed(6);
    37. console.log(latLngData)
    38. $.ajax({
    39. type:"post",
    40. url:"https://maps.googleapis.com/maps/api/geocode/json?latlng="+latLngData+"&location_type=ROOFTOP&result_type=street_address&key=AIzaSyB6oFVGAOKiES5J2ZyDuuXAfOdqIcpFpNA",
    41. async:true,
    42. success:function(data){
    43. console.log(data)
    44. var site = latLngData+'<br />'+data.results[0].formatted_address;
    45. console.log(site)
    46. var infowindow = new google.maps.InfoWindow({
    47. content: site
    48. });
    49. infowindow.open(map,marker); //弹出信息提示窗口
    50. }
    51. });
    52. });
    53. //添加坐标对象
    54. function addMarker(latLng, map) {
    55. if(markersArray.length>0){
    56. markersArray[0].setMap(null);
    57. };
    58. markersArray.shift(marker)
    59. marker = new google.maps.Marker({
    60. position: latLng,
    61. map: map
    62. });
    63. markersArray.push(marker);
    64. }
    65. }
    66. </script>
    67. <script src="http://cdn.bootcss.com/jquery/2.2.3/jquery.js"></script>
    68. <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB6oFVGAOKiES5J2ZyDuuXAfOdqIcpFpNA&callback=initMap" async defer></script>
    69. </body>
    70. </html>