Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate Geolocation latitude and longitude

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

This should be validated. It must be validated so that:

  • The values are correctly formatted. E.g. 48.8582 and 2.2945 but also 48.8 and 2.2. Any precision should be allowed. I store them as Float, any addtitional validation needed or advised?
  • They are within valid ranges (0-90). Or should I allow negative numbers, or numbers above and below 90?

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.

like image 231
berkes Avatar asked Feb 11 '13 15:02

berkes


People also ask

How do I verify GPS location?

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.

How do I find a location using latitude and longitude coordinates?

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.

What are valid longitude and latitude values?

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.


1 Answers

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 }
like image 154
deivid Avatar answered Sep 28 '22 03:09

deivid