2011-08-29 54 views
1

任何人都可以有代码来编码折线(数组)经纬度值来将ASCII字符串在Java编码纬度和经度的折线为ascii值

为例如

我的数组是在Java

latlng{ 
    {22296401,70797251}, 
    {22296401,70797451}, 
    {22296401,70797851} 
} 

这上面的值保存到列表对象的GeoPoint类型像

List<GeoPoint> polyline 

,想转换为ASCII字符串这样

[email protected] 

我需要接受latlng数组的数组并返回ascii字符串的方法 任何帮助将提前欣赏感谢

+0

从经纬度/长度数字到ASCII字符串的算法是什么? – Jesper

+0

此链接用于将ascii字符串解码为latlng值的多段线数组http://www.geekyblogger.com/2010/12/decoding-polylines-from-google-maps.html – Pratik

+0

请注意,该文章包含[link (http://code.google.com/intl/nl-NL/apis/maps/documentation/utilities/polylinealgorithm.html)解释了如何进行编码。现在你只需要实现... – Jesper

回答

2

我从this post

这两功能的答案是需要的折线阵列编码成ASCII字符串

private static String encodeSignedNumber(int num) { 
    int sgn_num = num << 1; 
    if (num < 0) { 
     sgn_num = ~(sgn_num); 
    } 
    return(encodeNumber(sgn_num)); 
} 

private static String encodeNumber(int num) { 

    StringBuffer encodeString = new StringBuffer(); 

    while (num >= 0x20) { 
     encodeString.append((char)((0x20 | (num & 0x1f)) + 63)); 
     num >>= 5; 
    } 

    encodeString.append((char)(num + 63)); 

    return encodeString.toString(); 

} 

测试尝试的坐标从this网站和比较输出

这里是片段

StringBuffer encodeString = new StringBuffer(); 

String encode = Geo_Class.encodeSignedNumber(3850000)+""+Geo_Class.encodeSignedNumber(-12020000);      
encodeString.append(encode); 
encode = Geo_Class.encodeSignedNumber(220000)+""+Geo_Class.encodeSignedNumber(-75000);      
encodeString.append(encode); 
encode = Geo_Class.encodeSignedNumber(255200)+""+Geo_Class.encodeSignedNumber(-550300);      
encodeString.append(encode); 

Log.v("encode string", encodeString.toString()); 

从统筹链接,您知道这一点

Points: (38.5, -120.2), (40.7, -120.95), (43.252, -126.453) 

确定,所以现在你认为统筹是为什么不同的看到,当你得到新的坐标,那么你必须从以前的一个例如减

1. 3850000,-12020000 => 3850000,-12020000 
2. 4070000,-12095000 => (4070000 - 3850000),(-12095000 - -12020000) => +220000, -75000 

是看重你必须传递给encodeSignedNumber()方法,你会得到的是统筹

等ASCII值....