Parent

Included Modules

Geokit::LatLng

Attributes

lat[RW]
lng[RW]

Public Class Methods

from_json(json) click to toggle source
# File lib/geokit/lat_lng.rb, line 16
def self.from_json(json)
  new(json['lat'], json['lng'])
end
from_string(thing) click to toggle source
# File lib/geokit/lat_lng.rb, line 103
def self.from_string(thing)
  thing.strip!
  if match=thing.match(/(\-?\d+\.?\d*)[, ] ?(\-?\d+\.?\d*)$/)
    Geokit::LatLng.new(match[1], match[2])
  else
    res = Geokit::Geocoders::MultiGeocoder.geocode(thing)
    return res if res.success?
    raise Geokit::Geocoders::GeocodeError
  end
end
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 lib/geokit/lat_lng.rb, line 9
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
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 responding to to_lat_lng -- a LatLng will be extracted from it
# File lib/geokit/lat_lng.rb, line 83
def self.normalize(thing, other=nil)
  return Geokit::LatLng.new(thing, other) if other

  case thing
  when String
    from_string(thing)
  when Array
    thing.size == 2 or raise ArgumentError.new("Must initialize with an Array with both latitude and longitude")
    Geokit::LatLng.new(thing[0], thing[1])
  when LatLng # will also be true for GeoLocs
    thing
  else
    if thing.respond_to? :to_lat_lng
      thing.to_lat_lng
    else
      raise ArgumentError.new("#{thing} (#{thing.class}) cannot be normalized to a LatLng. We tried interpreting it as an array, string, etc., but no dice.")
    end
  end
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 lib/geokit/lat_lng.rb, line 56
def ==(other)
  return false unless other.is_a?(LatLng)
  lat == other.lat && lng == other.lng
end
eql?(other) click to toggle source
# File lib/geokit/lat_lng.rb, line 65
def eql?(other)
  self == other
end
hash() click to toggle source
# File lib/geokit/lat_lng.rb, line 61
def hash
  lat.hash + lng.hash
end
lat=(lat) click to toggle source

Latitude attribute setter; stored as a float.

# File lib/geokit/lat_lng.rb, line 21
def lat=(lat)
  @lat = lat.to_f if lat
end
lat_dms() click to toggle source

returns latitude as [ degree, minute, second ] array

# File lib/geokit/lat_lng.rb, line 36
def lat_dms
  self.class.decimal_to_dms(lat)
end
ll() click to toggle source

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

# File lib/geokit/lat_lng.rb, line 31
def ll
  "#{lat},#{lng}"
end
lng=(lng) click to toggle source

Longitude attribute setter; stored as a float;

# File lib/geokit/lat_lng.rb, line 26
def lng=(lng)
  @lng=lng.to_f if lng
end
lng_dms() click to toggle source

returns longitude as [ degree, minute, second ] array

# File lib/geokit/lat_lng.rb, line 41
def lng_dms
  self.class.decimal_to_dms(lng)
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 # => #<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...>

# File lib/geokit/lat_lng.rb, line 127
def reverse_geocode(options = { :using => Geokit::Geocoders::MultiGeocoder })
  if options[:using].is_a?(String) || options[:using].is_a?(Symbol)
    provider = Geokit::Geocoders.const_get("#{Geokit::Inflector::camelize(options[:using].to_s)}Geocoder")
  elsif options[:using].respond_to?(:do_reverse_geocode)
    provider = options[:using]
  else
    raise ArgumentError.new("#{options[:using]} is not a valid geocoder.")
  end

  provider.send(:reverse_geocode, self)
end
to_a() click to toggle source

returns a two-element array

# File lib/geokit/lat_lng.rb, line 51
def to_a
  [lat,lng]
end
to_s() click to toggle source

returns a string with comma-separated lat,lng values

# File lib/geokit/lat_lng.rb, line 46
def to_s
  ll
end
valid?() click to toggle source

Returns true if both lat and lng attributes are defined

# File lib/geokit/lat_lng.rb, line 70
def valid?
  lat && lng
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.