Class: Geokit::LatLng
- Inherits:
-
Object
- Object
- Geokit::LatLng
- Includes:
- Mappable
- Defined in:
- lib/geokit/mappable.rb
Direct Known Subclasses
Constant Summary
Constants included from Mappable
Mappable::EARTH_RADIUS_IN_KMS, Mappable::EARTH_RADIUS_IN_MILES, Mappable::EARTH_RADIUS_IN_NMS, Mappable::KMS_PER_LATITUDE_DEGREE, Mappable::KMS_PER_MILE, Mappable::LATITUDE_DEGREES, Mappable::MILES_PER_LATITUDE_DEGREE, Mappable::NMS_PER_LATITUDE_DEGREE, Mappable::NMS_PER_MILE, 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.
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#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.
-
#reverse_geocode(options = { :using => Geokit::Geocoders::MultiGeocoder }) ⇒ Object
Reverse geocodes a LatLng object using the MultiGeocoder (default), or optionally using a geocoder of your choosing.
-
#to_a ⇒ Object
returns a two-element array.
-
#to_s ⇒ Object
returns a string with comma-separated lat,lng values.
-
#valid? ⇒ Boolean
Returns true if both lat and lng attributes are defined.
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
223 224 225 226 227 228 |
# File 'lib/geokit/mappable.rb', line 223 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.
219 220 221 |
# File 'lib/geokit/mappable.rb', line 219 def lat @lat end |
#lng ⇒ Object
Returns the value of attribute lng.
219 220 221 |
# File 'lib/geokit/mappable.rb', line 219 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
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/geokit/mappable.rb', line 282 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 elsif thing.respond_to? :to_lat_lng return thing.to_lat_lng end raise 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.
256 257 258 |
# File 'lib/geokit/mappable.rb', line 256 def ==(other) other.is_a?(LatLng) ? self.lat == other.lat && self.lng == other.lng : false end |
#eql?(other) ⇒ Boolean
264 265 266 |
# File 'lib/geokit/mappable.rb', line 264 def eql?(other) self == other end |
#hash ⇒ Object
260 261 262 |
# File 'lib/geokit/mappable.rb', line 260 def hash lat.hash + lng.hash end |
#ll ⇒ Object
Returns the lat and lng attributes as a comma-separated string.
241 242 243 |
# File 'lib/geokit/mappable.rb', line 241 def ll "#{lat},#{lng}" end |
#reverse_geocode(options = { :using => Geokit::Geocoders::MultiGeocoder }) ⇒ Object
Reverse geocodes a LatLng object using the MultiGeocoder (default), or optionally using a geocoder of your choosing. Returns a new Geokit::GeoLoc object
Options
-
:using - Specifies the geocoder to use for reverse geocoding. Defaults to
MultiGeocoder. Can be either the geocoder class (or any class that implements do_reverse_geocode for that matter), or the name of the class without the "Geocoder" part (e.g. :google)
Examples
LatLng.new(51.4578329, 7.0166848).reverse_geocode # => #<Geokit::GeoLoc:0x12dac20 @state…> LatLng.new(51.4578329, 7.0166848).reverse_geocode(:using => :google) # => #<Geokit::GeoLoc:0x12dac20 @state…> LatLng.new(51.4578329, 7.0166848).reverse_geocode(:using => Geokit::Geocoders::GoogleGeocoder) # => #<Geokit::GeoLoc:0x12dac20 @state…>
321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/geokit/mappable.rb', line 321 def reverse_geocode( = { :using => Geokit::Geocoders::MultiGeocoder }) if [:using].is_a?(String) or [:using].is_a?(Symbol) provider = Geokit::Geocoders.const_get("#{Geokit::Inflector::camelize([:using].to_s)}Geocoder") elsif [:using].respond_to?(:do_reverse_geocode) provider = [:using] else raise ArgumentError.new("#{[:using]} is not a valid geocoder.") end provider.send(:reverse_geocode, self) end |
#to_a ⇒ Object
returns a two-element array
251 252 253 |
# File 'lib/geokit/mappable.rb', line 251 def to_a [lat,lng] end |
#to_s ⇒ Object
returns a string with comma-separated lat,lng values
246 247 248 |
# File 'lib/geokit/mappable.rb', line 246 def to_s ll end |
#valid? ⇒ Boolean
Returns true if both lat and lng attributes are defined
269 270 271 |
# File 'lib/geokit/mappable.rb', line 269 def valid? self.lat and self.lng end |