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 (It’s 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)

HTTP status code o 200 . The table could look like this: CREATE TABLE IF NOT EXISTS `buildings` ( `building_id` INT NOT NULL AUTO_INCREMENT . `building_street` VARCHAR(200) NULL . 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. PRIMARY KEY (`building_id`) ) Then our PHP script for filling up GPS coordinates could look like this: .-73. `building_longitude` VARCHAR(30) NULL .730070. `building_city` VARCHAR(200) NOT NULL .42.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. Longitude For example: 200. `building_street_nr` VARCHAR(20) NULL . Latitude 4.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.  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. `building_latitude` VARCHAR(30) NULL .6.

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

0 mysql_close($conn). . ?> In following article will be introduced MySQL procedure which returns list of the nearest buildings calculated from GPS.

Sign up to vote on this title
UsefulNot useful