// js 的解密方法
function decode(C) {
var digi=16;
var add= 10;
var plus=7;
var cha=36;
var I = -1;
var H = 0;
var B = "";
var J = C.length;
var G = C.charCodeAt(J - 1);
C = C.substring(0, J - 1);
J--;
for (var E = 0; E < J; E++) {
var D = parseInt(C.charAt(E), cha) - add;
if (D >= add) {
D = D - plus
}
B += (D).toString(cha);
if (D > H) {
I = E;
H = D
}
}
var A = parseInt(B.substring(0, I), digi);
var F = parseInt(B.substring(I + 1), digi);
var L = (A + F - parseInt(G)) / 2;
var K = (F - L) / 100000;
L /= 100000;
return {
lat: K,
lng: L
}
}
document.write(decode('HHDFJGZVVIHIJG').lat+','+decode('HHDFJGZVVIHIJG').lng);
// 结果是 39.09917,117.22348
上面这个 JS 方法是大众点评用来解密自己的 POI 变成 坐标信息的,我想用 Ruby 的方法实现,但是我尝试写了,两个方法的输出结果总是不对,由于我对 js 不太熟悉,请大家看看我错在哪里?
def decode(poi)
digi=16
add= 10
plus=7
cha=36
i = -1
h = 0
b = ""
j = poi.length
g = poi[-1].ord
poi = poi[0..j-2]
j -= 1
for n in (0..j-1) do
puts n
d = poi[n].to_i(cha) - add
if d >= add
d = d - plus
end
b += d.to_s(cha)
if d > h
i = n
h = d
end
end
a = b[0..i-1].to_i(digi)
f = b[i..-1].to_i(digi)
l = (a + f - g.to_i) / 2
k = (f - l) / 100000.0
l /= 100000.0
return {lat:k ,lng:l}
end