| Class | GeoKit::LatLng |
| In: |
vendor/plugins/geokit/lib/geo_kit/mappable.rb
|
| Parent: | Object |
| lat | [RW] | |
| lng | [RW] |
Accepts latitude and longitude or instantiates an empty instance if lat and lng are not provided. Converted to floats if provided
# File vendor/plugins/geokit/lib/geo_kit/mappable.rb, line 194
194: def initialize(lat=nil, lng=nil)
195: lat = lat.to_f if lat && !lat.is_a?(Numeric)
196: lng = lng.to_f if lng && !lng.is_a?(Numeric)
197: @lat = lat
198: @lng = lng
199: end
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 vendor/plugins/geokit/lib/geo_kit/mappable.rb, line 240
240: def self.normalize(thing,other=nil)
241: # if an 'other' thing is supplied, normalize the input by creating an array of two elements
242: thing=[thing,other] if other
243:
244: if thing.is_a?(String)
245: thing.strip!
246: if match=thing.match(/(\-?\d+\.?\d*)[, ] ?(\-?\d+\.?\d*)$/)
247: return GeoKit::LatLng.new(match[1],match[2])
248: else
249: res = GeoKit::Geocoders::MultiGeocoder.geocode(thing)
250: return res if res.success
251: raise GeoKit::Geocoders::GeocodeError
252: end
253: elsif thing.is_a?(Array) && thing.size==2
254: return GeoKit::LatLng.new(thing[0],thing[1])
255: elsif thing.is_a?(LatLng) # will also be true for GeoLocs
256: return thing
257: elsif thing.class.respond_to?(:acts_as_mappable) && thing.class.respond_to?(:distance_column_name)
258: return thing.to_lat_lng
259: end
260:
261: 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.")
262: end
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 vendor/plugins/geokit/lib/geo_kit/mappable.rb, line 227
227: def ==(other)
228: other.is_a?(LatLng) ? self.lat == other.lat && self.lng == other.lng : false
229: end
Latitude attribute setter; stored as a float.
# File vendor/plugins/geokit/lib/geo_kit/mappable.rb, line 202
202: def lat=(lat)
203: @lat = lat.to_f if lat
204: end
Returns the lat and lng attributes as a comma-separated string.
# File vendor/plugins/geokit/lib/geo_kit/mappable.rb, line 212
212: def ll
213: "#{lat},#{lng}"
214: end
Longitude attribute setter; stored as a float;
# File vendor/plugins/geokit/lib/geo_kit/mappable.rb, line 207
207: def lng=(lng)
208: @lng=lng.to_f if lng
209: end