LinkedIn Twitter RSS Reset

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.


<?php
class googleRequest {

  var $gKey;
  var $code;
  var $Accuracy;
  var $latitude;
  var $longitude;
  var $address;
  var $city;
  var $country;
  var $error;

  function GetRequest() {

    if (strlen($this->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" ;
    }
  }

}

?>

The above class can be implemented as following.

$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);

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″ }

7 Responses to “Latitude and Longitude Of a Place in Google Maps Using Zip Code”

  1. Netemp
    November 3, 2008 at 6:56 am #

    Hi Subesh,

    You have created an awesome script!!

    Thanks alot!

  2. Netemp
    December 1, 2008 at 12:34 pm #

    Hi Subesh,

    I have used your script in my project but sometimes it returns Latitude and Longitude as 0, 0 for few locations.

    When these locations are searched at Google Map then there are User Created Pointers shown which indicate the location.

    Could you help me resolve this issue that what whould be done when Latitudw and Longitude are returned as 0, 0.

    Thanks in advance.

  3. December 3, 2008 at 9:48 am #

    I think you might have mis-requested zip codes !

  4. December 7, 2008 at 11:44 am #

    fantastic example and have done this all. but need another requirement. i want to do search for uk street using postcode. is that possible? if yes then is there any example?

  5. Tejas
    April 16, 2010 at 6:44 am #

    Hi subesh
    I gave zip codes of my town and other towns nearby, but it gives me result as 0. Also for some codition the lattitude and longitude doesnt show any maps,
    Please help
    thanks in advance

    • Subesh Pokhrel
      April 16, 2010 at 12:19 pm #

      What was your query string?

      Try like this http://maps.google.com/maps/geo?q=35005%20&output=xml (You need to Reload this url, don’t know why to see the result)

      This will give you in XML format. You can test just by pasting in browser url box. The code is just to set the query string and read CSV. Should work! I’ve tested it already. Just check your query string.

      Thanks.

  6. September 6, 2011 at 8:09 am #

    This will work if you have placed the correct address. it would be better if you find lat and long based on Post code/Zip code…

Leave a Comment