Subesh Pokhrel's Blog – Magento Development Tips,PHP,Google Maps
PHP & Magento Tips & Tutorials
Share

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

  • Share/Bookmark

You may also be interested

Comments

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

  1. Hi Subesh,

    You have created an awesome script!!

    Thanks alot!

  2. 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. I think you might have mis-requested zip codes !

  4. 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?

Write a Comment

Search engine optimization by SEO Design Solutions