Class: ServerSideGoogleMaps::Path
- Inherits:
-
Object
- Object
- ServerSideGoogleMaps::Path
- Defined in:
- lib/server-side-google-maps/path.rb
Instance Attribute Summary collapse
-
#points ⇒ Object
readonly
Returns the value of attribute points.
Class Method Summary collapse
Instance Method Summary collapse
-
#calculate_distances ⇒ Object
Sets .distance on every Point in the Path.
-
#elevations(n) ⇒ Object
Returns a new Path with n equidistant Points along this Path, complete with .elevation.
-
#initialize(points) ⇒ Path
constructor
A new instance of Path.
-
#interpolate(n) ⇒ Object
Returns a Path that approximates this one, with n points.
- #simplify(latlng_error2) ⇒ Object
Constructor Details
#initialize(points) ⇒ Path
Returns a new instance of Path.
10 11 12 13 14 15 16 17 18 |
# File 'lib/server-side-google-maps/path.rb', line 10 def initialize(points) raise ArgumentError.new('path must be an Enumerable') unless Enumerable === points raise ArgumentError.new('path must have one or more points') unless points.length > 0 i = 0 @points = points.collect do |pt| raise ArgumentError.new("path element #{i} must have .latitude and .longitude") unless pt.respond_to?(:latitude) pt end end |
Instance Attribute Details
#points ⇒ Object (readonly)
Returns the value of attribute points.
3 4 5 |
# File 'lib/server-side-google-maps/path.rb', line 3 def points @points end |
Class Method Details
.get_elevations(params) ⇒ Object
5 6 7 8 |
# File 'lib/server-side-google-maps/path.rb', line 5 def self.get_elevations(params) server = Server.new server.get('/maps/api/elevation', {:sensor => false}.merge(params)) end |
Instance Method Details
#calculate_distances ⇒ Object
Sets .distance on every Point in the Path
Yes, this is a hack: it assumes all the Points will only belong to this Path. It’s here because it’s convenient.
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/server-side-google-maps/path.rb', line 43 def calculate_distances total_distance = 0 last_point = nil points.each do |point| if last_point distance_piece = last_point.distance(point) total_distance += distance_piece end point.distance_along_path = total_distance last_point = point end end |
#elevations(n) ⇒ Object
Returns a new Path with n equidistant Points along this Path, complete with .elevation
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/server-side-google-maps/path.rb', line 21 def elevations(n) results = self.class.get_elevations(:path => "enc:#{encoded_path}", :samples => n) i = -1 total_distance = points.last.distance_along_path points = results['results'].collect do |r| i += 1 Point.new( r['location']['lat'].to_f, r['location']['lng'].to_f, :elevation => r['elevation'].to_f, :distance_along_path => total_distance && (total_distance.to_f * i / (n - 1)).to_i ) end Path.new(points) end |
#interpolate(n) ⇒ Object
Returns a Path that approximates this one, with n points
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/server-side-google-maps/path.rb', line 57 def interpolate(n) calculate_distances unless points[0].distance_along_path total_distance = points[-1].distance_along_path - points[0].distance_along_path distance_step = 1.0 * total_distance / (n - 1) ret = [] ret << create_interpolated_point(points[0], points[0], 0.0, :distance_along_path => points[0].distance_along_path) j = 0 current_distance = points[0].distance_along_path (1...(n - 1)).each do |i| current_distance += distance_step if i > 0 while j < (points.length - 2) && points[j + 1].distance_along_path < current_distance #&& points[j].distance_along_path == points[j + 1].distance_along_path j += 1 end point_before = points[j] point_after = points[j + 1] point_segment_length = point_after.distance_along_path - point_before.distance_along_path fraction_after = (1.0 * current_distance - point_before.distance_along_path) / point_segment_length ret << create_interpolated_point(point_before, point_after, fraction_after, :distance_along_path => current_distance.to_i) end ret << create_interpolated_point(points[-1], points[-1], 0.0, :distance_along_path => points[-1].distance_along_path) Path.new(ret) end |