Google Maps API level: calculates the line length

Source: Internet
Author: User
Tags polyline
From: http://hi.baidu.com/xfm_zhr/blog/item/fbfe5c274a87a904908f9d28.html

1.How to encode longitude and latitude

For static data that does not need to be modified, you can use the online encoding tool provided by Google to obtain the data. For more information, see related documentation.

For data that needs to be dynamically generated, it can be encoded according to the Code provided by GoogleAlgorithmDescription: it is used for encoding. For encoding instructions, refer to the Google documentation.

2.Calculate the length of a broken line

Basic Idea: Use the Earth as a perfect sphere. Calculate the distance (arc length, radius * radians) between two adjacent points of the line and accumulate the distance.

Because the above method adopts approximate processing, the obtained value is for reference only.

Several glatlng methods:

Glatlng. LAT (); // returns the latitude of the angle value.

Glatlng. LNG (); // returns the longitude of the angle value.

Glatlng. latradians (); // returns the latitude of the radian value.

Glatlng. lngradians (); // returns the longitude of the radian value.

Example of calculating the arc length:

Function getdistance2 (from, to, unit ){

//Earth radius

VaR r = 6378.137;

//Kilometer-mile Conversion

VaR M = 1.609344;

With (math)

{

VaR LAT1 = from. latradians ();

VaR lng1 = from. lngradians ();

VaR LAT2 = to. latradians ();

VaR lng2 = to. lngradians ();

VaR Dist = 2 * asin (SQRT (POW (sin (LAT1-LAT2)/2), 2) + cos (LAT1) * Cos (LAT2) * POW (sin (lng1-lng2)/2), 2) * R;

}

If (unit = "M ")

{

Dist = DIST/m;

}

Return Dist;

}

Example of calculating the line length:

Function getdistance (){

VaR Dist = 0;

//Total number of vertices on a line

VaR COUNT = polyline. getvertexcount ();

//Calculate the distance from all vertices

For (VAR I = 0; I <count-1; I ++ ){

Dist + = getdistance2 (polyline. getvertex (I), polyline. getvertex (I + 1 ));

}

Return Dist;

}

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.