jigniter™

jQuery & CodeIgniter – Perfect combination for web application

Get Location Information based on an address, city or country name

sin comentarios

In our last post we explained how to use GeoIP easily in CodeIgniter. Now we are interested in getting location information from a particular address using a Geocoder tool. For that reason, we’d recommend to take a look over How to use Google Maps API with PHP from Zimuel’s blog.

Zimuel exposes the following scenario:

Scenario: you have to check the validity of the geo data of a list of customers stored into a database. Using the Google Maps API you can easly compare the data stored into your database with the google data and fix the errors.

Moreover, his solution using Google Maps API proposes the following solution.

A PHP class that uses the Google Maps API to provide geographic information from a generic address (city, country, street+city, country. etc.).

So, based on that basis we would be interested in integrating this solution into CodeIgniter application. In order to do that, we can achieve the following steps:

1. Downloading the GMaps library from Zimuel’s blog.

2. Unpacking the zip and copy the GMaps.php file into your library directory: /application/libraries/GMaps.php

3. Put the content under example.php into your controller class where you are interested in using the Google Maps library. For example, you can use the following code to get information from a location assuming that $gmaps_key is your Google Maps key (get it from here for free), and $search is the variable containing the search string (ie: an address, a city, etc.:


        $this->load->library('GMaps', $gmaps_key );
        if ($this->gmaps->getInfoLocation( $search )) {

            echo 'Address: '.$this->gmaps->getAddress().'';
            echo 'Country name: '.$this->gmaps->getCountryName().'';
            echo 'Country name code: '.$this->gmaps->getCountryNameCode().'';
            echo 'Administrative area name: '.$this->gmaps->getAdministrativeAreaName().'';
            echo 'Postal code: '.$this->gmaps->getPostalCode().'';
            echo 'Latitude: '.$this->gmaps->getLatitude().'';
            echo 'Longitude: '.$this->gmaps->getLongitude().'';

            $this->data['lat'] = $this->gmaps->getLatitude();
            $this->data['lng'] = $this->gmaps->getLongitude();

        } else {
            echo "The response of Google Maps is empty";
        }

Finally, the text displayed in the page contains geo information such as latitude, longitude, postal code, country code and name, etc.

Escrito por admin

2010-01-10 at 3:48 pm

Publicado en Snippets Ideas

Tags: ,

Leave a Reply