Google Map V3 javascript calculates the distance between two marker
For map development, some operations and interactions of marker are most commonly used. A brief introduction is the distance calculation between two marker.
Google map api is very convenient, as long as it is commonly used, there are basically interfaces.
1. Create two marker points
Copy codeThe Code is as follows: 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 Database Copy codeThe Code is as follows: <script type = "text/javascript" src = "http://maps.googleapis.com/maps/api/js? Sensor = false & libraries = geometry "> </script>
Libr: libraries = geometry
3. Calculate the distance Copy codeThe Code is as follows: var meters = google. maps. geometry. spherical. computeDistanceBetween (oldMarker. getPosition (), newMarker. getPosition ());
Document. getElementById ("distance"). innerText = meters + "meter ";
Description: The unit is meters.
OldMarker. getPosition () gets the current location of oldmarker (longitude and latitude)