Lat, lng points to GMmaps polyline in Ruby

February 11th, 2008 | by matt |

def encode(num)
  str = “”
  num = (num * 1e5).floor
  num <<= 1
  num = ~num if num < 0
  while num >= 0×20
    str += (((num & 0×1f) | 0×20) + 63).chr
    num >>= 5
  end
  str += (num+63).chr
  return str
end

def to_encoded_polyline(points)
  polyline = “”
  points.each do |point|
    polyline += encode(point[:lat])
    polyline += encode(point[:lng])
  end
  return polyline
end