Module: Geokit::Mappable
- Included in:
- LatLng
- Defined in:
- lib/geokit/mappable.rb
Overview
Contains class and instance methods providing distance calcuation services. This module is meant to be mixed into classes containing lat and lng attributes where distance calculation is desired.
At present, two forms of distance calculations are provided:
-
Pythagorean Theory (flat Earth) - which assumes the world is flat and loses accuracy over long distances.
-
Haversine (sphere) - which is fairly accurate, but at a performance cost.
Distance units supported are :miles, :kms, and :nms.
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.included(receiver) ⇒ Object
Mix below class methods into the includer.
Instance Method Summary collapse
-
#distance_to(other, options = {}) ⇒ Object
(also: #distance_from)
Returns the distance from another point.
-
#endpoint(heading, distance, options = {}) ⇒ Object
Returns the endpoint, given a heading (in degrees) and distance.
-
#heading_from(other) ⇒ Object
Returns heading in degrees (0 is north, 90 is east, 180 is south, etc) FROM the given point.
-
#heading_to(other) ⇒ Object
Returns heading in degrees (0 is north, 90 is east, 180 is south, etc) to the given point.
-
#midpoint_to(other, options = {}) ⇒ Object
Returns the midpoint, given another point on the map.
-
#to_lat_lng ⇒ Object
Extracts a LatLng instance.
Class Method Details
.included(receiver) ⇒ Object
Mix below class methods into the includer.
17 18 19 |
# File 'lib/geokit/mappable.rb', line 17 def self.included(receiver) # :nodoc: receiver.extend ClassMethods end |
Instance Method Details
#distance_to(other, options = {}) ⇒ Object Also known as: distance_from
Returns the distance from another point. The other point parameter is required to have lat and lng attributes. Valid options are: :units - valid values are :miles, :kms, :or :nms (:miles is the default) :formula - valid values are :flat or :sphere (:sphere is the default)
201 202 203 |
# File 'lib/geokit/mappable.rb', line 201 def distance_to(other, = {}) self.class.distance_between(self, other, ) end |
#endpoint(heading, distance, options = {}) ⇒ Object
Returns the endpoint, given a heading (in degrees) and distance. Valid option: :units - valid values are :miles, :kms, or :nms (:miles is the default)
223 224 225 |
# File 'lib/geokit/mappable.rb', line 223 def endpoint(heading, distance, = {}) self.class.endpoint(self, heading, distance, ) end |
#heading_from(other) ⇒ Object
Returns heading in degrees (0 is north, 90 is east, 180 is south, etc) FROM the given point. The given point can be a LatLng or a string to be Geocoded
216 217 218 |
# File 'lib/geokit/mappable.rb', line 216 def heading_from(other) self.class.heading_between(other, self) end |
#heading_to(other) ⇒ Object
Returns heading in degrees (0 is north, 90 is east, 180 is south, etc) to the given point. The given point can be a LatLng or a string to be Geocoded
209 210 211 |
# File 'lib/geokit/mappable.rb', line 209 def heading_to(other) self.class.heading_between(self, other) end |
#midpoint_to(other, options = {}) ⇒ Object
Returns the midpoint, given another point on the map. Valid option: :units - valid values are :miles, :kms, or :nms (:miles is the default)
230 231 232 |
# File 'lib/geokit/mappable.rb', line 230 def midpoint_to(other, = {}) self.class.midpoint_between(self, other, ) end |
#to_lat_lng ⇒ Object
Extracts a LatLng instance. Use with models that are acts_as_mappable
188 189 190 191 192 193 194 195 |
# File 'lib/geokit/mappable.rb', line 188 def to_lat_lng if instance_of?(Geokit::LatLng) || instance_of?(Geokit::GeoLoc) return self end lat = send(self.class.lat_column_name) lng = send(self.class.lng_column_name) LatLng.new(lat, lng) end |