javascript - 我如何通过让客户在谷歌地图中放置标记来获取客户的纬度和位置?

标签 javascript jquery html google-maps google-maps-api-3

我有 android 的 api。但是在网站上,我如何通过向他提供谷歌地图让他放置标记来获取他的位置?

是否有任何标准的使用方法?我需要获取这些纬度和经度并传递给后端。我怎样才能达到同样的目的?我是第一次做

最佳答案

你可以使用google map的click事件获取纬度和经度,如下所示:

 function initMap() {
  var uluru = {
    lat: -25.363,
    lng: 131.044
  };
  var mapDiv = document.getElementById('map');
  var map = new google.maps.Map(mapDiv, {
    zoom: 8,
    center: uluru,
    clickableIcons: true,
  });

  google.maps.event.addListener(map, 'click', function(e) {

    if (map.getClickableIcons()) {
      var Latitude = e.latLng.lat();
      var Longitude = e.latLng.lng();
      // add your new marker with this info.
      var marker = new google.maps.Marker({
        position: {
          lat: Latitude,
          lng: Longitude
        },
        map: map,
        draggable: true,
      });
      map.clickableIcons = false;
      // get latitude and longitude after dragging:
      google.maps.event.addListener(marker, 'dragend', 
      function(marker){
           var latLng = marker.latLng; 
           currentLatitude = latLng.lat();
           currentLongitude = latLng.lng();

 }); 


    }
  });
}
/* Always set the map height explicitly to define the size of the div
 * element that contains the map. */

#map {
  height: 100%;
}

/* Optional: Makes the sample page fill the window. */

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCu8vqg35dD4fNfxlI8ICoycehbL0F6bis&callback=initMap">
</script>

Learn more about map's dom-listners

关于javascript - 我如何通过让客户在谷歌地图中放置标记来获取客户的纬度和位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47787587/

相关文章:

javascript - Typescript 3.7@beta 可选链接运算符使用问题

javascript - 如何访问 Protractor 中 promise 内声明的变量

javascript - 使用 Javascript 在 PHP 中选择选项时启用按钮

javascript - 如何优化这个 jQuery 代码?

html - 如何在页面不被页眉和页脚重叠的情况下在 div 中加载页面?

javascript - 如何使用 jquery 重置表单中的多个选择框?

javascript - 循环处理时 onClick 中重复 id

javascript - 停止页面滚动到指定的 Div(或页脚元素)

html - jQuery Mobile 中的标签宽度

html - css 边框不适用于 chrome 中的鼠标悬停事件?