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

Instance Method Summary collapse

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)



219
220
221
# File 'lib/geokit/mappable.rb', line 219

def distance_to(other, options = {})
  self.class.distance_between(self, other, options)
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)



241
242
243
# File 'lib/geokit/mappable.rb', line 241

def endpoint(heading, distance, options = {})
  self.class.endpoint(self, heading, distance, options)
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



234
235
236
# File 'lib/geokit/mappable.rb', line 234

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



227
228
229
# File 'lib/geokit/mappable.rb', line 227

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)



248
249
250
# File 'lib/geokit/mappable.rb', line 248

def midpoint_to(other, options = {})
  self.class.midpoint_between(self, other, options)
end

#to_lat_lngObject

Extracts a LatLng instance. Use with models that are acts_as_mappable



206
207
208
209
210
211
212
213
# File 'lib/geokit/mappable.rb', line 206

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