我有 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/