Checking latitude and longitude

I have a Ruby on Rails Camping model with the attributes #latitude and #longitude .

This must be confirmed. It must be checked so that:

  • Values ​​are formatted correctly. For instance. 48.8582 and 2.2945, as well as 48.8 and 2.2. Any accuracy should be allowed. I store them as a float, is any additional verification necessary or recommended?
  • They are in acceptable ranges (0-90). Or should I allow negative numbers or numbers above and below 90?

I am not interested in whether this item is a valid valid address (sea, north pole, whatever, if it is a valid point on earth).

I am using a geocoder gem, but at the moment the input is just a pair of lat / lon text fields. The geocoder is just interested in this question because it can have useful methods (which I cannot find) for checking the lat / long pair.

+5
source share
2 answers

I don't know about any methods in Geocoder to test this, but you can just use rail checks

 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 } 
+19
source

In Rails 4 and above, the correct answer would be:

 validates :lat, numericality: { greater_than_or_equal_to: -90, \ less_than_or_equal_to: 90 } validates :lng, numericality: { greater_than_or_equal_to: -180, \ less_than_or_equal_to: 180 } 
0
source

All Articles