forked from nayutaya/googlemaps-polyline
/
encoder.rb
56 lines (45 loc) · 956 Bytes
/
encoder.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# coding: utf-8
module GoogleMapsPolyline
class Encoder
def initialize(io)
@io = io
end
attr_reader :io
def encode_points(points)
plat, plng = 0, 0
points.each { |lat, lng|
@io.write(pack_point(lat - plat))
@io.write(pack_point(lng - plng))
plat, plng = lat, lng
}
return @io
end
private
def pack_point(num)
negative = (num < 0)
if negative
num -= -1
num *= -1
end
num <<= 1
num |= 1 if negative
codes = []
begin
code = (num & 0b11111) + 63
num >>= 5
code += 0x20 if num > 0
codes << code
end while num > 0
return codes.pack("C*")
end
def pack_level(level)
case level
when 0 then return "?"
when 1 then return "@"
when 2 then return "A"
when 3 then return "B"
else raise(ArgumentError)
end
end
end
end