Home > Web Front-end > JS Tutorial > Javascript calculates the distance between two markers (Google Map V3)_javascript skills

Javascript calculates the distance between two markers (Google Map V3)_javascript skills

WBOY
Release: 2016-05-16 17:35:14
Original
1716 people have browsed it

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

Copy the code The 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 library
Copy code The code is as follows:



Instructions: libraries=geometry
3. Calculate distance
Copy code The code is as follows:

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)
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template