Google Map V3 javascript calculates the distance between two markers
When developing maps, the most commonly used ones are some marker operations and interactions. Let’s briefly introduce the distance calculation between two markers.
Google map api is very convenient. As long as it is commonly used, it basically has interfaces.
1. Create two marker points
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. Load the geometry library
Instructions: libraries=geometry
3. Calculate distance
var meters = google.maps.geometry.spherical.computeDistanceBetween(oldMarker.getPosition(), newMarker.getPosition ());
document.getElementById("distance").innerText = meters "meters";
Description: The unit is meters
oldMarker.getPosition() Get the current position of oldmarker (latitude and longitude)