Implementation of Google Maps API encoding line algorithm

Source: Internet
Author: User
Tags binary to decimal decimal to binary

Implementation of Google Maps API encoding line algorithm

 

 

In the secondary development of Google Maps API, we encountered the encoding line algorithm problem through gpolyline. fromencoded can effectively improve the rendering speed. based on the fact that there is no relevant implementation on the internet, I will give the algorithm of latitude and longitude and level in the encoding line for your reference. it is implemented using MFC and Standard C. You can modify the relevant code in other environments.

 

 

1. Convert binary to decimal (8 digits)

 

Inline int bintodec (char * B)
{
Int I, j, V;
V = 0;
Int Len = strlen (B );
If (LEN <1) return V;
I = 0; V = 0; j = 0;
For (I = len-1; I> = 0; I --)
{
If (B [I] = '1 ')
V + = (INT) Pow (2, J );
J ++;
}
Return V;
}

 

Ii. Related Algorithms

 

1. Latitude and longitude Algorithms

Cstring polyline_lonlat_encode (double num)
{
Int m, I;
Cstring S;

Int d [8];

Char m_buf [8] [6];
Int n = 32;
Char Buf [256] = {0 };
Bool is_negative = false;
Double T = num;

// Fine-tune data to prevent data distortion
T + = (T <1e-8 )? -1:1) * 1e-8;
T * = 1e5;
M = int (t );
If (M <0) is_negative = true;

// Convert decimal to binary
For (I = 0; I <n; I ++)
{
If (1 <(n-i-1) & M)
Buf [I] = '1 ';
Else
Buf [I] = '0 ';
}

// Calculate the anticode and add the suffix 1 (negative number is 1, positive number is 0)
If (is_negative)
{
For (I = 0; I <n; I ++)
{
If (BUF [I] = '0 ')
Buf [I] = '1 ';
Else
Buf [I] = '0 ';

}
}
S = Buf;
S + = is_negative? "1": "0 ";
// Remove the front-end 0, starting from 1
I = S. Find ("1", 0 );
If (I! =-1)
S = S. Right (S. getlength ()-I );

Else

S = "0 ";

// Computing Group (a group of five digits)

M = S. getlength ()/5;
If (M * 5 <S. getlength ())
M + = 1;
// XOR 0x20 (not used in the last group) before grouping data, add 63
For (I = 0; I <m; I ++)
{
Sprintf (m_buf [I], "% s", S. Right (5 ));
If (I = m-1)
D [I] = bintodec (m_buf [I]) + 63;
Else
D [I] = bintodec (m_buf [I]) + 32 + 63;

S = S. Left (S. getlength ()-5 );
}

// Convert to ASCII
S = "";
For (I = 0; I <m; I ++)
{
S + = toascii (d [I]);
}
Return S;
}

2. encoding-level Algorithms

Cstring polyline_level_encode (INT level)

{
Int m, I;
Cstring S;

Int d [8];

Char m_buf [8] [6];
Int n = 32;
Char Buf [256] = {0 };

M = level;

// Convert decimal to binary
For (I = 0; I <n; I ++)
{
If (1 <(n-i-1) & M)
Buf [I] = '1 ';
Else
Buf [I] = '0 ';
}

// Calculate the anticode and add the suffix 1 (negative number is 1, positive number is 0)
If (is_negative)
{
For (I = 0; I <n; I ++)
{
If (BUF [I] = '0 ')
Buf [I] = '1 ';
Else
Buf [I] = '0 ';

}
}
S = Buf;
// Remove the front-end 0, starting from 1
I = S. Find ("1", 0 );
If (I! =-1)
S = S. Right (S. getlength ()-I );

Else

S = "0 ";

// Computing Group (a group of five digits)

M = S. getlength ()/5;
If (M * 5 <S. getlength ())
M + = 1;
// XOR 0x20 (not used in the last group) before grouping data, add 63
For (I = 0; I <m; I ++)
{
Sprintf (m_buf [I], "% s", S. Right (5 ));
If (I = m-1)
D [I] = bintodec (m_buf [I]) + 63;
Else
D [I] = bintodec (m_buf [I]) + 32 + 63;

S = S. Left (S. getlength ()-5 );
}

// Convert to ASCII
S = "";
For (I = 0; I <m; I ++)
{
S + = toascii (d [I]);
}
Return S;
}

 

Iii. Call description

 

Afxmessagebox ("level:" + polyline_level_encode (16 ));
Afxmessagebox ("points (38.5-120.2), (40.7,-120.95), (43.252,-126.453):" +

Polyline_lonlat_encode (38.5) + polyline_lonlat_encode (-120.2) +

Polyline_lonlat_encode (40.7-38.5) +

Polyline_lonlat_encode (-120.95 + 120.2) +

Polyline_lonlat_encode (43.252-40.7) +

Polyline_lonlat_encode (-126.453 + 120.95 ));

 

Iv. Description

1. For the encoding line algorithm format, see Encoding

2. sprintf, toascii, and pow are standard C functions.

 

Finally, I hope you can join us for more! My email: wxy3064one@163.com

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.