Class: ServerSideGoogleMaps::Directions

Inherits:
Object
  • Object
show all
Defined in:
lib/server-side-google-maps/directions.rb

Constant Summary collapse

LATLNG_STRING_REGEXP =
/(-?\d+(\.?\d+)?),(-?\d+(\.?\d+)?)/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(origin, destination, params = {}) ⇒ Directions

Initializes directions

Parameters: origin: string or [lat,lng] of the first point destination: string or [lat,lng] of the last point params:

:mode: :driving, :bicycling and :walking will be passed to Google Maps.
       Another option, :direct, will avoid in-between points and calculate
       the distance using the Haversine formula. Defaults to :driving.
:find_shortcuts: [ {:factor => Float, :mode => :a_mode}, ... ]
                 For each list item (in the order given), determines if
                 using given :mode will cut the distance to less than
                 :factor and if so, chooses it. For example, if :mode is
                 :bicycling and there's a huge detour because of a missing
                 bike lane, pass { :factor => 0.5, :mode => :driving }
                 and if a shortcut cuts the distance in half that route
                 will be chosen instead.

Raises:

  • (ArgumentError)


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
# File 'lib/server-side-google-maps/directions.rb', line 27

def initialize(origin, destination, params = {})
  @origin = origin
  @destination = destination
  params = params.dup
  find_shortcuts = params.delete(:find_shortcuts) || []
  raise ArgumentError.new(':find_shortcuts must be an Array') unless Array === find_shortcuts
  @direct = params[:mode] == :direct
  params[:mode] = :driving if params[:mode] == :direct || params[:mode].nil?

  origin = origin.join(',') if Array === origin
  destination = destination.join(',') if Array === destination

  unless @direct && origin_point_without_server && destination_point_without_server
    @data = self.class.get(params.merge(:origin => origin, :destination => destination))
  end

  if !@direct
    find_shortcuts.each do |try_shortcut|
      factor = try_shortcut[:factor]
      mode = try_shortcut[:mode]

      other = Directions.new(origin, destination, params.merge(:mode => mode))
      if other.distance.to_f / distance < factor
        @path = other.path
        @distance = other.distance
      end
    end
  end
end

Class Method Details

.get(params) ⇒ Object



5
6
7
8
# File 'lib/server-side-google-maps/directions.rb', line 5

def self.get(params)
  server = Server.new
  server.get('/maps/api/directions', {:sensor => false}.merge(params))
end

Instance Method Details

#destination_addressObject



69
70
71
# File 'lib/server-side-google-maps/directions.rb', line 69

def destination_address
  leg['end_address']
end

#destination_inputObject



61
62
63
# File 'lib/server-side-google-maps/directions.rb', line 61

def destination_input
  @destination
end

#destination_pointObject



77
78
79
# File 'lib/server-side-google-maps/directions.rb', line 77

def destination_point
  @destination_point ||= destination_point_without_server || Point.new(leg['end_location']['lat'], leg['end_location']['lng'])
end

#distanceObject



85
86
87
# File 'lib/server-side-google-maps/directions.rb', line 85

def distance
  @distance ||= calculate_distance
end

#origin_addressObject



65
66
67
# File 'lib/server-side-google-maps/directions.rb', line 65

def origin_address
  leg['start_address']
end

#origin_inputObject



57
58
59
# File 'lib/server-side-google-maps/directions.rb', line 57

def origin_input
  @origin
end

#origin_pointObject



73
74
75
# File 'lib/server-side-google-maps/directions.rb', line 73

def origin_point
  @origin_point ||= origin_point_without_server || Point.new(leg['start_location']['lat'], leg['start_location']['lng'])
end

#pathObject



89
90
91
# File 'lib/server-side-google-maps/directions.rb', line 89

def path
  @path ||= Path.new(points)
end

#statusObject



81
82
83
# File 'lib/server-side-google-maps/directions.rb', line 81

def status
  @data['status']
end