We've long been used to using Google's tools and services to do a lot of things... only good things come to an end and some of these services close or become paid for.
As part of the development of my Plugin OpenAgenda for WordPressI need to find the longitude and latitude of an event address.
OpenStreetMap to the rescue
Fortunately, there are many services that can replace those of Google. Among them OpenStreetMap (OSM) for everything that revolves around maps and geolocation.
Nominatim for the conversion of address to location data.
So I discovered the service Nominatim.
That's all very nice, but I want code!
If, like me, you need to retrieve the latitude and longitude of a geographic point in PHP... for WordPress, here's what I do:
A little explanation:
Line 1: we declare a function in which I pass by setting an address (street zip code city).
Line 2: I encode this address so that it can be integrated into a URL.
Line 3I'm checking to see if there's a transition to the coordinates. Nominatim restricts access to its IAP... the transient allows you to store information locally and not to make a request at each display... especially since the geographical coordinates of a point should not change too often...
Line 14: I'll create the settlement which will be valid for 90 days. Basically, your WordPress will only require geolocation once every 90 days for the same address.
Between 2 if the address has never been geocoded then I request the Nominatim API.
Any ideas or improvements to make? Leave me a comment!
Image à la Une : Brett Zeck
Don't miss any of my articles, subscribe to my newsletter
(1 mail per month if there is new content)