designing4u.de Yet Another Coding Blog

6May/086

Geolocation class – retrieving longitude and latitude using google maps

A while ago I had to prepare a map for a customer. He wanted to be able to use text files with addresses and display them on the map. To be able to do that I had to find a service, which will convert a regular address into geographical longitude and latitude. What would be better than google maps to do that really simple task? I researched a little bit on google and found a really nice way, how to accomplish that.

On Tim Shower's homepage I have found a really nice php geocoding tutorial, which I used to build my solution. For our purposes we will use client for URL (cURL) to retrieve the longitude and latitude of our places. Let's define our class first.
[source:php]
class Geolocation {

private $url = array();

}
[/source]
We define private array url, in which we will store URLs prepared for cURL request. Let's build a contructor.
[source:php]
class Geolocation {

private $url = array();

function __construct($file) {
$gmapkey = "YOURKEY";
$address = file($file);
foreach($address as $addres) {
$this->url[] = "http://maps.google.com/maps/geo?q=".urlencode($addres)."&output=csv&key=".$gmapkey;
}
}
}
[/source]
You need to replace $gmapkey with the key you can sign for at google. Our __constructor reads the text file and saves each line in an array. We then loop through this array and initialize URLs.

In the next method we will loop through the addresses to retrieve the geolocation.

[source:php]
class Geolocation {

private $url = array();

function __construct($file) {
$gmapkey = "YOURKEY";
$address = file($file);
foreach($address as $addres) {
$this->url[] = "http://maps.google.com/maps/geo?q=".urlencode($addres)."&output=csv&key=".$gmapkey;
}
}
public function getLatLng() {
$i=1;
foreach($this->url as $urls) {
$cinit = curl_init();
curl_setopt($cinit, CURLOPT_URL, $urls);
curl_setopt($cinit, CURLOPT_HEADER,0);
curl_setopt($cinit, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($cinit, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($cinit, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($cinit);
curl_close($cinit);

if(strstr($data,"200")) {
$data = explode(",",$data);
echo $i.": ";
$zoom = $data[1];
echo $lat = $data[2];
echo $lon = $data[3]."
";
}

/*
$sql = "INSERT INTO..."
$res = mysql_query($sql);
echo ($res) ? $i.": lat:".$latitude." lon: ".$longitdue." -- OK
" : $i.": ERROR
";
*/
$i++;
}
}
}
[/source]
You can either echo the results or just save them in your MySQL database. You can also make multidimensional array and save some extra information about the certain place.

Let's take a look at how you should use this class.
[source:php]
include('geolocation.class.php');
$geo = new Geolocation('address.txt');
$geo->getLatLng();
[/source]
Basically you need to include the class, initialize it with the name of the file you want to use and getLatLng will do the magic for you. This class is pretty old and you may encounter one problem using it. After 10th request I started to receive the same geolocation for different places. What you can do is to put the foreach loop to sleep for 2 seconds and it should be fine. That's why I actually use iterator in this function. You basically need to add this line right after the loop starts.
[source:php]
if($i%10 == 0) sleep(2);
[/source]

Here is the content of the text file:
[source:html]
Berlin, Dieffenbachstr. 38
Berlin, Cuvrystr. 32
Berlin, Lasdehner Str. 30
[/source]
Basically one address per line. That would be all, have fun with playing and extending this class.

Comments (6) Trackbacks (2)
  1. Consider it done Tim!

    Once again nice job with your tutorial!

  2. Great! This is what I was looking for!

  3. @Ruben

    You welcome :)

  4. Exactly what I was looking for, thanks!

    One note: The line that says “$response = curl_exec($cinit);” should actually have the response variable named “$data” because that’s what’s referred to in the code below it.

  5. Yeah, I saw it, but did find time yet to correct this.


Leave a comment


Pages

Categories

Blogroll

Archive

Meta