-2

I am building a project where it is up to the user to type in lat and long and I use that to convert into a google map as such:

<iframe width="100%" height="450" frameborder="0" style="border:0" src="https://www.google.com/maps/embed/v1/place?q=<?=$foo->getLocationLat()?>,<?=$foo->getLocationLong()?>&amp;key=<?=$foo->getAPIKey"></iframe>

But obviously we will have issues where the lat long they type in won't be valid, and as a result the google map will fail, etc. Is there an elegant solution to determine if the map is correct prior to painting it to the screen? I would like to not have to pass the lat long into a method to determine if the address exists (this is the only way I can think of doing this).

Zach Smith
  • 5,490
  • 26
  • 84
  • 139

2 Answers2

0

If you want to determine if a coordinate is valid easily, you could check if the latitude and longitude are within their respective ranges, as described here.

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.

Lastly, you mention something about address-validation. This can be a bit of a hurdle, as latitudes and longitudes do not necessarily map to specific addresses. Some coordinates will map to hundreds of addresses, while some will map to none. If you need to know whether or not a latitude and longitude might match an address, you can use the Google Geocoding API.

Hedam
  • 2,209
  • 27
  • 53
-1

Minimum & Maximum Longitude is +/- 180°. Minimum & Maximum Latitude is +/- 90°

Anything within this range is a valid location on the earth. If you're just looking for coordinates that won't throw an error on google, this should do it.

Whether or not it's a valid address for a home or business requires a database lookup. These are commercially available as downloads and as an API. I don't believe I've ever seen one for free, although it's certainly possible they exist.

Terry Carmen
  • 3,720
  • 1
  • 16
  • 32