Class: ServerSideGoogleMaps::Point
- Inherits:
-
Object
- Object
- ServerSideGoogleMaps::Point
- Defined in:
- lib/server-side-google-maps/point.rb
Constant Summary collapse
- RADIUS_OF_EARTH =
metres
6367000
- DEGREES_TO_RADIANS =
Math::PI / 180
Instance Attribute Summary collapse
-
#distance_along_path ⇒ Object
Returns the value of attribute distance_along_path.
-
#elevation ⇒ Object
readonly
Returns the value of attribute elevation.
-
#latitude ⇒ Object
readonly
Returns the value of attribute latitude.
-
#longitude ⇒ Object
readonly
Returns the value of attribute longitude.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#distance(other) ⇒ Object
Calculates the distance to another point, in metres.
-
#initialize(latitude, longitude, options = {}) ⇒ Point
constructor
A new instance of Point.
- #latlng_distance_squared(other) ⇒ Object
- #latlng_distance_squared_from_segment(segment) ⇒ Object
Constructor Details
#initialize(latitude, longitude, options = {}) ⇒ Point
Returns a new instance of Point.
9 10 11 12 13 14 15 |
# File 'lib/server-side-google-maps/point.rb', line 9 def initialize(latitude, longitude, = {}) @latitude = latitude @longitude = longitude @object = [:object] if [:object] @distance_along_path = [:distance_along_path] if [:distance_along_path] @elevation = [:elevation] if [:elevation] end |
Instance Attribute Details
#distance_along_path ⇒ Object
Returns the value of attribute distance_along_path.
7 8 9 |
# File 'lib/server-side-google-maps/point.rb', line 7 def distance_along_path @distance_along_path end |
#elevation ⇒ Object (readonly)
Returns the value of attribute elevation.
6 7 8 |
# File 'lib/server-side-google-maps/point.rb', line 6 def elevation @elevation end |
#latitude ⇒ Object (readonly)
Returns the value of attribute latitude.
6 7 8 |
# File 'lib/server-side-google-maps/point.rb', line 6 def latitude @latitude end |
#longitude ⇒ Object (readonly)
Returns the value of attribute longitude.
6 7 8 |
# File 'lib/server-side-google-maps/point.rb', line 6 def longitude @longitude end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
6 7 8 |
# File 'lib/server-side-google-maps/point.rb', line 6 def object @object end |
Instance Method Details
#==(other) ⇒ Object
17 18 19 20 |
# File 'lib/server-side-google-maps/point.rb', line 17 def ==(other) return false unless Point === other latitude == other.latitude && longitude == other.longitude && object == other.object end |
#distance(other) ⇒ Object
Calculates the distance to another point, in metres
The method assumes the Earth is a sphere.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/server-side-google-maps/point.rb', line 25 def distance(other) lat1 = latitude * DEGREES_TO_RADIANS lat2 = other.latitude * DEGREES_TO_RADIANS dlat = lat2 - lat1 dlon = (longitude - other.longitude) * DEGREES_TO_RADIANS # Optimize a tad. This method is slow. sin_dlat = Math.sin(dlat / 2) sin_dlon = Math.sin(dlon / 2) a = sin_dlat * sin_dlat + Math.cos(lat1) * Math.cos(lat2) * sin_dlon * sin_dlon sqrt_a = Math.sqrt(a) c = 2 * Math.asin(1.0 < sqrt_a ? 1.0 : sqrt_a) (RADIUS_OF_EARTH * c).to_i end |
#latlng_distance_squared(other) ⇒ Object
44 45 46 47 48 |
# File 'lib/server-side-google-maps/point.rb', line 44 def latlng_distance_squared(other) latitude_difference = latitude - other.latitude longitude_difference = longitude - other.longitude latitude_difference * latitude_difference + longitude_difference * longitude_difference end |
#latlng_distance_squared_from_segment(segment) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/server-side-google-maps/point.rb', line 50 def latlng_distance_squared_from_segment(segment) p1 = segment.first vx = p1.latitude - latitude vy = p1.longitude - longitude return vx*vx + vy*vy if segment.length2 == 0 ux = segment.dlat uy = segment.dlon det = (-vx*ux) + (-vy*uy) length2 = segment.length2 if det < 0 || det > length2 p2 = segment.last # We're outside the line segment wx = p2.latitude - latitude wy = p2.longitude - longitude d1 = vx*vx + vy*vy d2 = wx*wx + wy*wy return d1 < d2 ? d1 : d2 end det = ux*vy - uy*vx return det * det / length2 end |