Parent

Included Modules

Geokit::LatLng

Attributes

lat[RW]

(Not documented)

lng[RW]

(Not documented)

Public Class Methods

new(lat=nil, lng=nil) click to toggle source

Accepts latitude and longitude or instantiates an empty instance if lat and lng are not provided. Converted to floats if provided

     # File /Users/andre/projects/rails/geokit/lib/geokit/mappable.rb, line 217
217:     def initialize(lat=nil, lng=nil)
218:       lat = lat.to_f if lat && !lat.is_a?(Numeric)
219:       lng = lng.to_f if lng && !lng.is_a?(Numeric)
220:       @lat = lat
221:       @lng = lng
222:     end
normalize(thing,other=nil) click to toggle source

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
     # File /Users/andre/projects/rails/geokit/lib/geokit/mappable.rb, line 271
271:     def self.normalize(thing,other=nil)
272:       # if an 'other' thing is supplied, normalize the input by creating an array of two elements
273:       thing=[thing,other] if other
274:       
275:       if thing.is_a?(String)
276:         thing.strip!
277:         if match=thing.match(/(\-?\d+\.?\d*)[, ] ?(\-?\d+\.?\d*)$/)
278:           return Geokit::LatLng.new(match[1],match[2])
279:         else
280:           res = Geokit::Geocoders::MultiGeocoder.geocode(thing)
281:           return res if res.success?
282:           raise Geokit::Geocoders::GeocodeError  
283:         end
284:       elsif thing.is_a?(Array) && thing.size==2
285:         return Geokit::LatLng.new(thing[0],thing[1])
286:       elsif thing.is_a?(LatLng) # will also be true for GeoLocs
287:         return thing
288:       elsif thing.class.respond_to?(:acts_as_mappable) && thing.class.respond_to?(:distance_column_name)
289:         return thing.to_lat_lng
290:       end
291:       
292:       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.")
293:     end

Public Instance Methods

==(other) click to toggle source

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.

     # File /Users/andre/projects/rails/geokit/lib/geokit/mappable.rb, line 250
250:     def ==(other)
251:       other.is_a?(LatLng) ? self.lat == other.lat && self.lng == other.lng : false
252:     end
eql?(other) click to toggle source

(Not documented)

     # File /Users/andre/projects/rails/geokit/lib/geokit/mappable.rb, line 258
258:     def eql?(other)
259:       self == other
260:     end
hash() click to toggle source

(Not documented)

     # File /Users/andre/projects/rails/geokit/lib/geokit/mappable.rb, line 254
254:     def hash
255:       lat.hash + lng.hash
256:     end
lat=(lat) click to toggle source

Latitude attribute setter; stored as a float.

     # File /Users/andre/projects/rails/geokit/lib/geokit/mappable.rb, line 225
225:     def lat=(lat)
226:       @lat = lat.to_f if lat
227:     end
ll() click to toggle source

Returns the lat and lng attributes as a comma-separated string.

     # File /Users/andre/projects/rails/geokit/lib/geokit/mappable.rb, line 235
235:     def ll
236:       "#{lat},#{lng}"
237:     end
lng=(lng) click to toggle source

Longitude attribute setter; stored as a float;

     # File /Users/andre/projects/rails/geokit/lib/geokit/mappable.rb, line 230
230:     def lng=(lng)
231:       @lng=lng.to_f if lng
232:     end
reverse_geocode(options = { :using => Geokit::Geocoders::MultiGeocoder }) click to toggle source

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 # => # LatLng.new(51.4578329, 7.0166848).reverse_geocode(:using => :google) # => # LatLng.new(51.4578329, 7.0166848).reverse_geocode(:using => Geokit::Geocoders::GoogleGeocoder) # => #

     # File /Users/andre/projects/rails/geokit/lib/geokit/mappable.rb, line 308
308:     def reverse_geocode(options = { :using => Geokit::Geocoders::MultiGeocoder })
309:       if options[:using].is_a?(String) or options[:using].is_a?(Symbol)
310:         provider = Geokit::Geocoders.const_get("#{Geokit::Inflector::camelize(options[:using].to_s)}Geocoder")
311:       elsif options[:using].respond_to?(:do_reverse_geocode)
312:         provider = options[:using]
313:       else
314:         raise ArgumentError.new("#{options[:using]} is not a valid geocoder.")
315:       end
316:       
317:       provider.send(:reverse_geocode, self)
318:     end
to_a() click to toggle source

returns a two-element array

     # File /Users/andre/projects/rails/geokit/lib/geokit/mappable.rb, line 245
245:     def to_a
246:       [lat,lng]
247:     end
to_s() click to toggle source

returns a string with comma-separated lat,lng values

     # File /Users/andre/projects/rails/geokit/lib/geokit/mappable.rb, line 240
240:     def to_s
241:       ll
242:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.