微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当按位或具有破坏​​性时,如何解码谷歌折线?

如何解决当按位或具有破坏​​性时,如何解码谷歌折线?

我想解码 encoded google polyline

`~oia@

然而,要反转其中一个步骤需要反转按位 OR 操作,即 destructive

我看到它在这里完成:How to decode Google's Polyline Algorithm? 但我看不到如何在 Javascript 中做到这一点。

这是我目前所拥有的:

const partialDecodedpolyline = "`~oia@".split('').map(char => (char.codePointAt()-63).toString(2))

console.log(partialDecodedpolyline)

下一步是反转按位或...这怎么可能?

解决方法

有一个图书馆https://github.com/mapbox/polyline/blob/master/src/polyline.js

/*
  https://github.com/mapbox/polyline/blob/master/src/polyline.js
*/
const decode = function(str,precision) {
    var index = 0,lat = 0,lng = 0,coordinates = [],shift = 0,result = 0,byte = null,latitude_change,longitude_change,factor = Math.pow(10,Number.isInteger(precision) ? precision : 5);

    // Coordinates have variable length when encoded,so just keep
    // track of whether we've hit the end of the string. In each
    // loop iteration,a single coordinate is decoded.
    while (index < str.length) {

        // Reset shift,result,and byte
        byte = null;
        shift = 0;
        result = 0;

        do {
            byte = str.charCodeAt(index++) - 63;
            result |= (byte & 0x1f) << shift;
            shift += 5;
        } while (byte >= 0x20);

        latitude_change = ((result & 1) ? ~(result >> 1) : (result >> 1));

        shift = result = 0;

        do {
            byte = str.charCodeAt(index++) - 63;
            result |= (byte & 0x1f) << shift;
            shift += 5;
        } while (byte >= 0x20);

        longitude_change = ((result & 1) ? ~(result >> 1) : (result >> 1));

        lat += latitude_change;
        lng += longitude_change;

        coordinates.push([lat / factor,lng / factor]);
    }

    return coordinates;
};

console.log(decode("`~oia@"));

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。