Latitude and Longitude of a Place in Google Maps Using Zip Code
I have already posted similar post for finding out the latitude and longitude of a place using Google maps, but when I studied the traffic details and found out that when people visit that post, they sometime come with keywords like zipcode+latitude and longitudes, so I thought it would be better to them to post another article, giving them what they exactly want.
Before I write down the codes there is something we should know. Google provides a service of returning CSV (Comma Seperated Values) when requested to http://maps.google.com/maps/geo?q=$query&output=csv&key=$key. The $query may contain address, zip code, city and country. The service also outputs in other formats. But we have choosed CSV format because it would be easy for us to explode the values separated by commas. The output type is defined by the output parameter in the URL. The output or response we then receive are in the format of geocode, accuracy, latitude, longitude. $key is the Google maps API key.
[sourcecode=”php”]
gKey) > 1) {
$q = str_replace(’ ‘, ‘_’, $this->address.’,’.$this->zip.’+’.$this->city.’,’.$this->country);
if ($d = @fopen(“http://maps.google.com/maps/geo?q=$q&output=csv&key=”.$this->gKey, “r”)) {
$gcsv = @fread($d, 30000);
@fclose($d);
$output=array();
$tmp = explode(“,”, $gcsv);
// $this->code = $tmp[0];
// $this->Accuracy = $tmp[1];
$output[0]=$this->latitude = $tmp[2];
$output[1]=$this->longitude = $tmp[3];
return $output;
} else {
$error = “NO_CONNECTION” ;
}
} else {
$error = “No Google Maps Api Key” ;
}
}
}
?>
[/sourcecode]
The above class can be implemented as following.
[sourcecode=”php”]
$obj_google=new googleRequest;
$obj_google->zip=35005;
/* alternate uses
$obj_google->country=”Country Name Here”;
$obj_google->city=”City Name Here”;
$obj_google->address=”address”;
*/
$obj_google->gKey=”ABQIAAAAPHLcOOGHX2-uLk3K8q1nMRTkUAbhgKwL1jWWfpv-KGJeCrct7hTsLLnZdnZjzehmRIkaePagQvKNbw”;
$latlng=$obj_google->GetRequest();
var_dump($latlng);
[/sourcecode]
The above returns latitude and longitude of place with zip code 35005 as array(2) { [0]=> string(9) “33.592857” [1]=> string(10) “-86.994015” }