Вы находитесь на странице: 1из 4

PHP/Google Maps: How to get GPS coordinates for address

Posted in: Programming|By: Matou Havlena|17.09.20093 Comments

This article contains code example of searching and saving GPS coordinates for an addresses stored in the database. For this purpose we will use Google Maps API and Geocoding Service (Geocoding is process of converting addresses into geographic coordinates). The geocoding service may only be used in conjunction with displaying results on a Google map.

Geocoding Service
Geocoding Service provides way to get geographic coordinates via an HTTP request. We must send a request to http://maps.google.com/maps/geo? with several parameteres. We are interested in these parameters:
q (required) the address to which you want get coordinates key (required) your API key (you can obtain it here) sensor (required) true if the request is sent from a device with location sensor, otherwise false output (required) the format of output (the options are xml, kml, csv or json) oe (optional) output encoding (Its recommended set this parameter to utf8)

a dal

The input data should be encoded in utf-8.

CSV output format


Output data can be generated in these formats:
json kml

(default)

xml csv

data are separated by semi-colon

In our situation is CSV ideal because it returns only 4 blocks of data separated by semicolon: 1. HTTP status code o 200 - succes o 500 server error o 602 unknown address o 610 bad API key o 620 too many queries (>5000 per day or too many requests in too short a period of time) o a dal 2. Accuracy o 0 unknown o 1 country level o o 4 city (village) leel o 5 ZIP level o 6 -street level o 7 intersection level o 8 address level o 9 building level 3. Latitude 4. Longitude For example: 200,6,42.730070,-73.90570

PHP: example of getting GPS coordinates from CSV file


We assume having table in db with buildings addresses to which we can find their GPS coordinates. The table could look like this: CREATE TABLE IF NOT EXISTS `buildings` ( `building_id` INT NOT NULL AUTO_INCREMENT , `building_street` VARCHAR(200) NULL , `building_street_nr` VARCHAR(20) NULL , `building_city` VARCHAR(200) NOT NULL , `building_latitude` VARCHAR(30) NULL , `building_longitude` VARCHAR(30) NULL , PRIMARY KEY (`building_id`) ) Then our PHP script for filling up GPS coordinates could look like this:

1 <?php 2 $conn = mysql_connect("localhost", "user", "passwd"); 3 if (!$conn) { 4 die("Could not connect: ". mysql_error()); 5} 6 if (!mysql_select_db("database", $conn)) { 7 die("Could select db: ". mysql_error()); 8} 9 1 // YOUR DOMAIN API KEY 0 $api_key = "ABCDEFGHIJK"; 1 1 $query = "select * from buildings where building_latitude is null and 1 building_longitude is null order by building_id"; 2 $result = mysql_query($query); 1 3 while ($row = mysql_fetch_array($result)) { 1 4 // SET ADDRESS 1 $address = urlencode($row["building_street"]." ".$row["building_street_nr"]." 5 ".$row["building_city"]." Czech republic"); 1 6 // URL TO HTTP REQUEST 1 $link = 7 "http://maps.google.com/maps/geo?q=".$address."&key=".$api_key."&sensor=false& 1 output=csv&oe=utf8"; 8 1 // WE GET FILE CONTENT 9 $page = file_get_contents($link); 2 0 // WE OBTAIN DATA FROM GIVEN CSV 2 list($status, $accuracy, $latitude, $longitude) = explode(",", $page); 1 2 // IF EVERYTHING OK AND ACCURANCY GREATER THEN 3 WE SAVE 2 COORDINATES 2 if (($status == 200) and ($accuracy>=4)) { 3 $query_edit = "update buildings set building_latitude = '".$latitude."', 2 building_longitude = '".$longitude."' 4 where building_id = '".$row["building_id"]."'"; 2 $result_edit = mysql_query($query_edit); 5 echo $row["building_id"]." - OK<br />"; 2 } else { 6 echo $row["building_id"]." - ERROR<br />"; 2 } 7 2 // TIMER BECAUSE GOOGLE DOESN'T LIKE TO BE QUERIED IN SHORT 8 TIME 2 sleep(3); 9} 3

0 mysql_close($conn); ?>

In following article will be introduced MySQL procedure which returns list of the nearest buildings calculated from GPS.