Nature, science, photography and other things that interest me.

Ruby Polyline Encoder for Google Maps

I wrote this translation of the PolylineEncoder.js by Mark McClure because I needed a way to create encoded polylines for Google maps. When I first started my project – http://nomadexplorers.com.

I tried using an existing ruby translation of PolylineEncoder.js. It was giving me some errors when I tried using it and it only took an array of coordinates. For my needs I needed the class to accept an array of objects. This seems a bit more elegant to me. Also, in the notes that where provided in the class comments, it appeared to have a bug that would incorrectly encode the second point. So instead of trying to debug and rewrite their code, I just decided to do my own implementation.

This is an exact line for line port of the javascript version by Mark McClure that uses the Douglas-Peucker algorithm. You would use it the same way as the javascript version. Below is an example on how you might use it in your own ruby app. To use it with rails just put it inside your lib directory.

# A simple point class example:
# You will probably want to use something different
# Remember dp_encode accepts an array of objects

class Point
attr_accessor :lat, :lng
def initialize(lat, lng)
@lat, @lng = lat, lng
end
end

points = Array.new
points << Point.new(36.97005, -93.29783)
points << Point.new(36.97005, -93.29410000000001)
points << Point.new(36.96998, -93.28758)

# Actual usage of the Polyline Encoder
polyline_encoder = PolylineEncoder.new
polyline_encoder.dp_encode(points)

polyline_encoder.encoded_points
polyline_encoder.encoded_levels

I am also working on the decoder so you can easily decode an encoded polyline. Check back soon I am almost done. If you would like to use the Ruby class in your own projects, feel free to do so.

Download – polyline_encoder.rb.

6 Responses

  1. Todd

    I believe the method you’re using to compute distance is off because it assumes a flat world using a cartesian distance method rather then a spherical distance calculation.

    Here’s a pretty good example:
    http://jan.ucc.nau.edu/~cvm/latlon_formula.html

    November 27, 2007 at 6:17 pm

  2. Nico

    Duh ? we don’t care about the spherical distance, this calculation is for display purpose only, and on screen, earth is flat, especially on mercator projection. Where can i find the cartesian version ?

    December 20, 2007 at 11:53 am

  3. I have an implementation of a gpolyline encoder in ruby on the rubyforge site:

    https://rubyforge.org/projects/gpolyline-enc

    Cheers,

    Joel

    January 23, 2008 at 8:27 pm

  4. Sergey

    Hi and thanks for the code. Do you have a decoder as well? You wrote that you already have it but from code I can not see it.

    February 16, 2008 at 6:38 am

  5. George Lantz

    Sure do! http://georgelantz.com/2007/08/29/ruby-polyline-decoder-for-google-maps/

    February 16, 2008 at 6:49 pm

  6. satish

    Hello Sir
    I am doing project,Customizing WorldWind SDK, I faced so much problem while run the problem.Finally i ran successfully.But now i had problem is

    *1* While run the project i didn’t get the placename,but i am in netconnection
    Exception is :
    Feb 26, 2008 10:19:17 AM gov.nasa.worldwind.util.BasicNetworkStatus isNetworkUnavailable
    INFO: Public network appears to be unreachable

    *2* How to add placename to the desired place in the globe.I didn’t have any idea,please send how to add which class and package is used.

    I am wait for ur reply

    February 26, 2008 at 1:48 am

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>