【问题标题】:Encode the polyline of latitude and longitude to ascii value将经纬度折线编码为ascii值
【发布时间】:2011-11-05 21:39:01
【问题描述】:

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

例如

我的数组在 java 中

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

上面的值作为 GeoPoint 类型存储到 List 对象中

List<GeoPoint> polyline

想像这样转换成ascii字符串

a~l~Fjk~uOwHJy@P

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

【问题讨论】:

  • 从 lat/long 对的数字到那个 ASCII 字符串的算法是什么?
  • 此链接用于将ascii字符串解码为latlng值geekyblogger.com/2010/12/…的折线数组
  • 请注意,该文章包含一个link to a page,它解释了如何进行编码。现在你只需要实现它......
  • 我已经看到但无法实现它,我很难知道为什么我在这里问这个问题
  • 那么请详细说明您尝试了什么以及卡在哪里。人们不喜欢“请为我写这段代码”的问题......!

标签: java google-maps polyline google-polyline


【解决方案1】:

我从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 站点的坐标并比较输出

这里是sn-p

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 值

等等……

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-22
    • 1970-01-01
    • 2012-10-30
    • 2013-07-05
    • 1970-01-01
    • 2021-09-26
    • 2017-05-23
    • 2021-06-03
    相关资源
    最近更新 更多