Jump to content

php code to convert postal code to longitude and latitude


sungpeng

Recommended Posts

<?php
// three parts to the querystring: q is address, output is the format, key is the GAPI
$key = 'your key';
$location = 'singapore'
$url = 'http://maps.google.com/maps/geo?q='.urlencode($location).'&output=xml&key='.$key;

sleep(1);


$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_VERBOSE,0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT,1);
// I'm telling google maps im using Chrome because this process will be automated in future uses 
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/0.3.154.9 Safari/525.19');

// capture anything in output so script wont break
ob_start();
$xml = curl_exec($ch);
ob_get_clean();

// check for errors
$error = curl_error($ch);

echo $xml;

?>

 

 

Result below

Queensland, Australia 200 geocode

Queensland, Australia

AUAustraliaQLD 145.774,-19.70

 

How to break apart the echo $xml so that one variable is longitude only, one variable is latitude only. Do you have any javascript to recommend also? Thank

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.