Tutorials

Geocoding an address without using Google Maps

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:

PHP

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 : unsplash-logoBrett Zeck

Don't miss any of my articles, subscribe to my newsletter

(1 mail per month if there is new content)

About the author

Avatar - Sebastien Serre

Sebastien Serre

Passionate about the web since the late 1990s, I have developed over the years skills in web design. Creating websites under WordPress since 2006 and developing exclusively on Wordpress since 2014. I will create the site orPlugin WordPress you need. Developer ofplugins, Core Contributor, Moderator on the WPFR forumWordPress Translation Manager, WordPress Translation Manager and speaker at WordCampmy life is punctuated by WordPress.

Leave a comment

Your email address will not be published. Required fields are indicated with *

This site uses Akismet to reduce undesirables. Find out more about how your feedback data is used.

ut Donec at sem, risus Aenean leo consequat. nunc vel, justo