Skip to content

Instantly share code, notes, and snippets.

@BharathMG
Last active May 11, 2017 21:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BharathMG/5d835d9e0e5bb878274b to your computer and use it in GitHub Desktop.
Save BharathMG/5d835d9e0e5bb878274b to your computer and use it in GitHub Desktop.
func EncodePolyline(coordinates []*Point) string {
if len(coordinates) == 0 {
return ""
}
factor := math.Pow(10, 5)
output := encode(coordinates[0].Lat, factor) + encode(coordinates[0].Lng, factor)
for i := 1; i < len(coordinates); i++ {
a := coordinates[i]
b := coordinates[i-1]
output += encode(a.Lat-b.Lat, factor)
output += encode(a.Lng-b.Lng, factor)
}
return output
}
func encode(oldCoordinate float64, factor float64) string {
coordinate := int(math.Floor(oldCoordinate*factor + 0.5))
coordinate = coordinate << 1
if coordinate < 0 {
coordinate = ^coordinate
}
output := ""
for coordinate >= 0x20 {
runeC := string((0x20 | (coordinate & 0x1f)) + 63)
output = output + runeC
coordinate >>= 5
}
runeC := string(coordinate + 63)
output = output + runeC
return output
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment