JavaScript calculates the distance between two marker (Google Map V3) _javascript Tips

Source: Internet
Author: User
Google Map V3 JavaScript calculates the distance between two marker
To do map development, the most commonly used is to marker some operations and interaction. Briefly, the distance between two marker is calculated.
Google Map API is very convenient as long as the commonly used are basically interfaces.
1. Create two marker points
Copy Code code 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. Loading Geometry library
Copy Code code as follows:

<script type= "Text/javascript" src= "http://maps.googleapis.com/maps/api/js?sensor=false&libraries= Geometry "></script>

Description: Libraries=geometry
3. Calculate the distance
Copy Code code as follows:

var meters = Google.maps.geometry.spherical.computeDistanceBetween (Oldmarker.getposition (), Newmarker.getposition ( ));
document.getElementById ("Distance"). innertext = meters+ "M";

Description: Unit is M
Oldmarker.getposition () Gets the current position of the Oldmarker (latitude and longitude)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.