Class: GeoKit::LatLng
- Inherits:
-
Object
- Object
- GeoKit::LatLng
- Includes:
- Mappable
- Defined in:
- lib/geo_kit/mappable.rb
Direct Known Subclasses
Constant Summary
Constants included from Mappable
Mappable::EARTH_RADIUS_IN_KMS, Mappable::EARTH_RADIUS_IN_MILES, Mappable::KMS_PER_LATITUDE_DEGREE, Mappable::KMS_PER_MILE, Mappable::LATITUDE_DEGREES, Mappable::MILES_PER_LATITUDE_DEGREE, Mappable::PI_DIV_RAD
Instance Attribute Summary collapse
-
#lat ⇒ Object
Returns the value of attribute lat.
-
#lng ⇒ Object
Returns the value of attribute lng.
Class Method Summary collapse
-
.normalize(thing, other = nil) ⇒ Object
A class method to take anything which can be inferred as a point and generate a LatLng from it.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Returns true if the candidate object is logically equal.
-
#initialize(lat = nil, lng = nil) ⇒ LatLng
constructor
Accepts latitude and longitude or instantiates an empty instance if lat and lng are not provided.
-
#ll ⇒ Object
Returns the lat and lng attributes as a comma-separated string.
-
#to_a ⇒ Object
returns a two-element array.
-
#to_s ⇒ Object
returns a string with comma-separated lat,lng values.
Methods included from Mappable
#distance_to, #endpoint, #heading_from, #heading_to, included, #midpoint_to, #to_lat_lng
Constructor Details
#initialize(lat = nil, lng = nil) ⇒ LatLng
Accepts latitude and longitude or instantiates an empty instance if lat and lng are not provided. Converted to floats if provided
192 193 194 195 196 197 |
# File 'lib/geo_kit/mappable.rb', line 192 def initialize(lat=nil, lng=nil) lat = lat.to_f if lat && !lat.is_a?(Numeric) lng = lng.to_f if lng && !lng.is_a?(Numeric) @lat = lat @lng = lng end |
Instance Attribute Details
#lat ⇒ Object
Returns the value of attribute lat.
188 189 190 |
# File 'lib/geo_kit/mappable.rb', line 188 def lat @lat end |
#lng ⇒ Object
Returns the value of attribute lng.
188 189 190 |
# File 'lib/geo_kit/mappable.rb', line 188 def lng @lng end |
Class Method Details
.normalize(thing, other = nil) ⇒ Object
A class method to take anything which can be inferred as a point and generate a LatLng from it. You should use this anything you’re not sure what the input is, and want to deal with it as a LatLng if at all possible. Can take:
1) two arguments (lat,lng)
2) a string in the format "37.1234,-129.1234" or "37.1234 -129.1234"
3) a string which can be geocoded on the fly
4) an array in the format [37.1234,-129.1234]
5) a LatLng or GeoLoc (which is just passed through as-is)
6) anything which acts_as_mappable -- a LatLng will be extracted from it
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/geo_kit/mappable.rb', line 238 def self.normalize(thing,other=nil) # if an 'other' thing is supplied, normalize the input by creating an array of two elements thing=[thing,other] if other if thing.is_a?(String) thing.strip! if match=thing.match(/(\-?\d+\.?\d*)[, ] ?(\-?\d+\.?\d*)$/) return GeoKit::LatLng.new(match[1],match[2]) else res = GeoKit::Geocoders::MultiGeocoder.geocode(thing) return res if res.success raise GeoKit::Geocoders::GeocodeError end elsif thing.is_a?(Array) && thing.size==2 return GeoKit::LatLng.new(thing[0],thing[1]) elsif thing.is_a?(LatLng) # will also be true for GeoLocs return thing elsif thing.class.respond_to?(:acts_as_mappable) && thing.class.respond_to?(:distance_column_name) return thing.to_lat_lng end throw ArgumentError.new("#{thing} (#{thing.class}) cannot be normalized to a LatLng. We tried interpreting it as an array, string, Mappable, etc., but no dice.") end |
Instance Method Details
#==(other) ⇒ Object
Returns true if the candidate object is logically equal. Logical equivalence is true if the lat and lng attributes are the same for both objects.
225 226 227 |
# File 'lib/geo_kit/mappable.rb', line 225 def ==(other) other.is_a?(LatLng) ? self.lat == other.lat && self.lng == other.lng : false end |
#ll ⇒ Object
Returns the lat and lng attributes as a comma-separated string.
210 211 212 |
# File 'lib/geo_kit/mappable.rb', line 210 def ll "#{lat},#{lng}" end |
#to_a ⇒ Object
returns a two-element array
220 221 222 |
# File 'lib/geo_kit/mappable.rb', line 220 def to_a [lat,lng] end |
#to_s ⇒ Object
returns a string with comma-separated lat,lng values
215 216 217 |
# File 'lib/geo_kit/mappable.rb', line 215 def to_s ll end |