Class: GoogleMapDirections::Directions

Inherits:
Object
  • Object
show all
Defined in:
lib/google_map_directions.rb

Constant Summary collapse

@@base_google_url =
'http://maps.googleapis.com/maps/api/directions/json?'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(origin, destination, sensor = false) ⇒ Directions

Returns a new instance of Directions.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/google_map_directions.rb', line 11

def initialize(origin, destination, sensor=false)      
  @origin = origin
  @destination = destination
  @sensor = sensor
  url = "#{@@base_google_url}&origin=#{@origin.gsub(/\s/,'+')}&destination=#{@destination.gsub(/\s/,'+')}&sensor=#{@sensor.to_s}"
  @json = JSON.parse(open(url).read)
  if status_check
    @legs = @json["routes"][0]["legs"][0]
    set_up_paths
  else
    @legs = nil
    @path = nil
  end
end

Instance Attribute Details

#destinationObject

Returns the value of attribute destination.



8
9
10
# File 'lib/google_map_directions.rb', line 8

def destination
  @destination
end

#jsonObject

Returns the value of attribute json.



8
9
10
# File 'lib/google_map_directions.rb', line 8

def json
  @json
end

#originObject

Returns the value of attribute origin.



8
9
10
# File 'lib/google_map_directions.rb', line 8

def origin
  @origin
end

#pathObject

Returns the value of attribute path.



8
9
10
# File 'lib/google_map_directions.rb', line 8

def path
  @path
end

#sensorObject

Returns the value of attribute sensor.



8
9
10
# File 'lib/google_map_directions.rb', line 8

def sensor
  @sensor
end

Instance Method Details

#destination_addressObject



50
51
52
# File 'lib/google_map_directions.rb', line 50

def destination_address
  return @legs['end_address'] unless !status_check
end

#destination_coordinatesObject



42
43
44
# File 'lib/google_map_directions.rb', line 42

def destination_coordinates
  return @legs['end_location'] unless !status_check
end

#distance_as_stringObject



34
35
36
# File 'lib/google_map_directions.rb', line 34

def distance_as_string
  return @legs["distance"]["text"] unless !status_check
end

#distance_in_metersObject



38
39
40
# File 'lib/google_map_directions.rb', line 38

def distance_in_meters
  return @legs["distance"]["value"] unless !status_check
end

#duration_as_stringObject



59
60
61
# File 'lib/google_map_directions.rb', line 59

def duration_as_string
  return @legs['duration']['text'] unless !status_check    
end

#duration_in_secondsObject



63
64
65
# File 'lib/google_map_directions.rb', line 63

def duration_in_seconds
  return @legs['duration']["value"] unless !status_check   
end

#origin_addressObject



46
47
48
# File 'lib/google_map_directions.rb', line 46

def origin_address
  return @legs['start_address'] unless !status_check
end

#origin_coordinatesObject



54
55
56
# File 'lib/google_map_directions.rb', line 54

def origin_coordinates
  return @legs['start_location'] unless !status_check
end

#path_lengthObject



67
68
69
# File 'lib/google_map_directions.rb', line 67

def path_length
  return @path.length unless !status_check
end

#polylineObject



30
31
32
# File 'lib/google_map_directions.rb', line 30

def polyline
  return @json["routes"][0]["overview_polyline"]["points"] unless !status_check
end

#statusObject



26
27
28
# File 'lib/google_map_directions.rb', line 26

def status
  return @json["status"]      
end

#step(number) ⇒ Object



71
72
73
# File 'lib/google_map_directions.rb', line 71

def step(number)
  return @path[number] unless !status_check
end