时间:2021-07-01 10:21:17 帮助过:30人阅读
var oldMarker = new google.maps.Marker({
position: new google.maps.LatLng("31.95678", "177.898673"),
map: map,
title:"old"
});
var newMarker = new google.maps.Marker({
position: new google.maps.LatLng("31.45678", "177.098673"),
map: map,
title:"new"
});
2.加载geometry库
代码如下:
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=geometry"></script>
说明:libraries=geometry
3.计算距离
代码如下:
var meters = google.maps.geometry.spherical.computeDistanceBetween(oldMarker.getPosition(), newMarker.getPosition());
document.getElementById("distance").innerText = meters+"米";
说明:单位是米
oldMarker.getPosition() 获取oldmarker的当前位置(经纬度)