将经纬度折线编码为ascii值 [英] Encode the polyline of latitude and longitude to ascii value
问题描述
任何人都可以使用代码将纬度和经度值的折线(数组)编码为Java中的ASCII字符串
anyone can have code to encode the polyline(array of ) latitude and longitude value to the ascii string in java
例如
我的数组在Java中
latlng{
{22296401,70797251},
{22296401,70797451},
{22296401,70797851}
}
此以上值作为GeoPoint类型存储到List对象中,例如
this above value store into the List object as GeoPoint type like
List<GeoPoint> polyline
并想要像这样转换为ascii字符串
and want to convert into ascii string like this
a~l~Fjk~uOwHJy@P
我需要接受latlng值数组并返回ascii字符串的方法 任何帮助将不胜感激,谢谢
I need method which accept array of latlng value and return the ascii string any help will be appreciate thanks in advance
推荐答案
this two function was needed to encode the polyline array into the ascii string
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();
}
要进行测试,请尝试从此网站获得的坐标并比较输出
for testing try the co-ordinate from this site and compare the output
这是代码段
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());
从坐标链接中您可以得到这一点
from the co-ordinate link you getting this point
Points: (38.5, -120.2), (40.7, -120.95), (43.252, -126.453)
好吧,现在您认为坐标是为什么当您获得新坐标时会看到不同的坐标,然后例如从上一个坐标中减去
ok so now you think the co-ordinate are why different see when you get new co-ordinate then you have subtract from the previous one for example
1. 3850000,-12020000 => 3850000,-12020000
2. 4070000,-12095000 => (4070000 - 3850000),(-12095000 - -12020000) => +220000, -75000
您必须将该值传递给encodeSignedNumber()方法,然后获取该坐标的ascii值
that value you have to pass to the encodeSignedNumber() method and you get the ascii value for that co-ordinate
以此类推....
这篇关于将经纬度折线编码为ascii值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!