PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store
Related Articles

Geocoding with PHP and the Google Maps API

Introduction

Geocoding is the process of finding the longitude and latitude of a given address or location. The Google Maps service gives developers and web site owners free access to their geocoder, both using the Google Maps JavaScript API, as well as using the web service. In this article I will show you how to access the geocoder web service using PHP so that it can be used in your own applications.

There are many applications for a geocoder and having access to a free, reliable and frequently updated geocoder is a huge help in web application development. Some examples of where a geocoder can be useful include:

  • Plotting a user-submitted address on a map (such as a property owner listing their house on a real estate web site).
  • Discovering the longitude and latitude so calculations can be performed (such as a finding other sets of coordinates within a given distance).
  • Verifying addresses before shipping orders from your ecommerce store.
  • Retrieving the properly formatted address string for the given location. This is very useful when you consider how inconsistent users can be when entering their address.

When using Google Maps to display maps on your web site, it is easy to access the JavaScript based geocoder, however this isn't always an adequate solution, especially when it comes to accessibility.

For example, if your application saves coordinates based on a user-submitted address using the JavaScript geocoder, then this solution will not work for users who have JavaScript disabled. To overcome this we can use the web service instead, meaning all requests to the geocoder are performed on the server side.

In this article we will develop several PHP 5 classes that will make it easy to query the geocoder and access the response.

In This Article


Additional Files