lib/gtfs/realtime/nearby.rb in gtfs-realtime-0.2.3 vs lib/gtfs/realtime/nearby.rb in gtfs-realtime-0.3.0

- old
+ new

@@ -1,15 +1,27 @@ module GTFS class Realtime module Nearby - def nearby(latitude, longitude) - # TODO: this math is terrible! It'll fail for various edge cases. - # (e.g. close to the poles, overlapping to the prime meridian) - # That said, it's an okay approximation within the United States. + def self.included(base) + base.send :include, InstanceMethods + base.extend ClassMethods + end - all.select do |stop| - (stop.latitude - latitude).abs < 0.01 && - (stop.longitude - longitude).abs < 0.01 + module InstanceMethods + def distance(latitude, longitude) + Math::sqrt((latitude - self.latitude)**2 + (longitude - self.longitude)**2) + end + end + + module ClassMethods + def nearby(latitude, longitude) + # TODO: this math is terrible! It'll fail for various edge cases. + # (e.g. close to the poles, overlapping to the prime meridian) + # That said, it's an okay approximation away from the poles/meridian. + + all.select do |item| + item.distance(latitude, longitude) < 0.01 + end end end end end end \ No newline at end of file