I have a Ruby on Rails model Camping
with #latitude
and #longitude
attributes.
This should be validated. It must be validated so that:
I am not interested in whether on this point is an actual valid address (sea, northpole, whatever, as long as it is a valid point on earth).
I am using geocoder gem, but for now, input is simply a pair of lat/lon textfields. Geocoder is merely of interest for this question because it may have utility-methods (which I cannot find) to validate a lat/long pair.
Find GPS Coordinates using Google Maps iPhone or Android users can follow these steps to get proper latitude and longitude: Go to Google Maps app on your Smartphone and enter the location for which you want coordinates. You can also tap the “My Location” icon to get your current location.
To find a location using its latitude and longitude on any device, just open Google Maps. On your phone or tablet, start the Google Maps app. On a computer, go to Google Maps in a browser. Then enter the latitude and longitude values in the search field — the same one you would ordinarily use to enter an address.
The valid range of latitude in degrees is -90 and +90 for the southern and northern hemisphere, respectively. Longitude is in the range -180 and +180 specifying coordinates west and east of the Prime Meridian, respectively.
I'm not aware of any methods in Geocoder to check that but you could just use rails validations
validates :latitude , numericality: { greater_than_or_equal_to: -90, less_than_or_equal_to: 90 }
validates :longitude, numericality: { greater_than_or_equal_to: -180, less_than_or_equal_to: 180 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With