Class: GoogleDistanceMatrix::PolylineEncoder

Inherits:
Object
  • Object
show all
Defined in:
lib/google_distance_matrix/polyline_encoder.rb,
lib/google_distance_matrix/polyline_encoder/delta.rb,
lib/google_distance_matrix/polyline_encoder/value_encoder.rb

Overview

Encodes a set of lat/lng pairs in to a polyline according to Google’s Encoded Polyline Algorithm Format.

See developers.google.com/maps/documentation/utilities/polylinealgorithm

Defined Under Namespace

Classes: Delta, ValueEncoder

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(array_of_lat_lng_pairs, delta: Delta.new, value_encoder: ValueEncoder.new) ⇒ PolylineEncoder

Initialize a new encoder

Arguments

array_of_lat_lng_pairs    - The array of lat/lng pairs, like [[lat, lng], [lat, lng], ..etc]
delta                     - An object responsible for rounding and calculate the deltas
                            between the given lat/lng pairs.
value_encoder             - After deltas are calculated each value is passed to the encoder
                            to be encoded in to ASCII characters

See Also:

  • encode


30
31
32
33
34
35
# File 'lib/google_distance_matrix/polyline_encoder.rb', line 30

def initialize(array_of_lat_lng_pairs, delta: Delta.new, value_encoder: ValueEncoder.new)
  @array_of_lat_lng_pairs = array_of_lat_lng_pairs
  @delta = delta
  @value_encoder = value_encoder
  @encoded = nil
end

Class Method Details

.encode(array_of_lat_lng_pairs) ⇒ Object

Encodes a set of lat/lng pairs

Example

encoded = PolylineEncoder.encode [[lat, lng], [lat, lng]]


16
17
18
# File 'lib/google_distance_matrix/polyline_encoder.rb', line 16

def self.encode(array_of_lat_lng_pairs)
  new(array_of_lat_lng_pairs).encode
end

Instance Method Details

#encodeObject

Encode and returns the encoded string



38
39
40
41
42
43
44
45
# File 'lib/google_distance_matrix/polyline_encoder.rb', line 38

def encode
  return @encoded if @encoded

  deltas = @delta.deltas_rounded @array_of_lat_lng_pairs
  chars_array = deltas.map { |v| @value_encoder.encode v }

  @encoded = chars_array.join
end