我需要得到两点之间的距离,根据公式是
但谷歌地图提供的数据是纬度,日志,我需要以米为单位的距离
var map;
function initialize() {
var myLatlng = new google.maps.LatLng(41.38, 2.18);
var myLatlng2 = new google.maps.LatLng(41.35, 2.18);
var myOptions = {
zoom: 13,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
var marker = new google.maps.Marker({
draggable: true,
position: myLatlng,
map: map,
title: "Your location"
});
var marker2 = new google.maps.Marker({
draggable: true,
position: myLatlng2,
map: map,
title: "Your location"
});
google.maps.event.addListener(map, 'click', function(event) {
alert(event.latLng);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map-canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?callback=myMap"></script>
<div id="map-canvas" style="border: 2px solid #3872ac;"></div>
要获得经纬度两点之间的距离,使用转换为javascript的Haversine公式如下。
GMaps3
在库下提供包含以下内容geometry
的实用程序google.maps.geometry.spherical
:要包含它,您可以通过以下方式完成:
然后你可以使用
google.maps.geometry.spherical.computeDistanceBetween
which:所以:
例子: