module Geokit # Bounds represents a rectangular bounds, defined by the SW and NE corners class Bounds # sw and ne are LatLng objects attr_accessor :sw, :ne # provide sw and ne to instantiate a new Bounds instance def initialize(sw,ne) raise ArgumentError if !(sw.is_a?(Geokit::LatLng) && ne.is_a?(Geokit::LatLng)) @sw,@ne=sw,ne end #returns the a single point which is the center of the rectangular bounds def center @sw.midpoint_to(@ne) end # a simple string representation:sw,ne def to_s "#{@sw.to_s},#{@ne.to_s}" end # a two-element array of two-element arrays: sw,ne def to_a [@sw.to_a, @ne.to_a] end # Returns true if the bounds contain the passed point. # allows for bounds which cross the meridian def contains?(point) point=Geokit::LatLng.normalize(point) res = point.lat > @sw.lat && point.lat < @ne.lat if crosses_meridian? res &= point.lng < @ne.lng || point.lng > @sw.lng else res &= point.lng < @ne.lng && point.lng > @sw.lng end res end # returns true if the bounds crosses the international dateline def crosses_meridian? @sw.lng > @ne.lng 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. def ==(other) other.is_a?(Bounds) ? self.sw == other.sw && self.ne == other.ne : false end # Equivalent to Google Maps API's .toSpan() method on GLatLng's. # # Returns a LatLng object, whose coordinates represent the size of a rectangle # defined by these bounds. def to_span lat_span = (@ne.lat - @sw.lat).abs lng_span = (crosses_meridian? ? 360 + @ne.lng - @sw.lng : @ne.lng - @sw.lng).abs Geokit::LatLng.new(lat_span, lng_span) end class <