I need to get the distance between two points, according to the formula is
but the data provided by google maps is lat, log and I need the distance in meters
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>
To obtain the distance between two points with latitude and longitude, use the Haversine formula that translated into javascript is as follows.
GMaps3
offers under librarygeometry
the utilitygoogle.maps.geometry.spherical
which contains:To include it you can do it through:
Then you can use
google.maps.geometry.spherical.computeDistanceBetween
which:So:
Example: