How can i get latitude and longitude from zip code in php?

The following PHP function will generate Latitude and Longitude from given zip code.

function getLnt($zip){
$url = "https://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($zip)."&key=[YOUR KEY]";
$result_string = file_get_contents($url);
$result = json_decode($result_string, true);
$result1[]=$result['results'][0];
$result2[]=$result1[0]['geometry'];
$result3[]=$result2[0]['location'];
return $result3[0];
}


To display output:

";
 echo "Longitude: ".$val['lng']."
"; ?>


In Action:

Converting from PostCode to map reference is far from accurate, but it can be done using the Google Maps API. You can get a Google Maps API key from Google by just asking for it, although you are limited to a certain number of requests each day.

Google Maps usually works through JavaScript, but it is possible to ask Google to return the data in JSON format and then use the PHP function json_decode() to decode the information into a usable array format. To get Google to return the data in JSON you must pass the parameter "output=json" in your query string.

The following function can take a postal code and convert it into longitude and latitude.

function getLatLong($code){
 $mapsApiKey = 'your-google-maps-api-key';
 $query = "http://maps.google.co.uk/maps/geo?q=".urlencode($code)."&output=json&key=".$mapsApiKey;
 $data = file_get_contents($query);
 // if data returned
 if($data){
  // convert into readable format
  $data = json_decode($data);
  $long = $data->Placemark[0]->Point->coordinates[0];
  $lat = $data->Placemark[0]->Point->coordinates[1];
  return array('Latitude'=>$lat,'Longitude'=>$long);
 }else{
  return false;
 }
}

The function can be used in the following way. To keep with the theme, the following two postal codes are one UK address and USA office locations of Google.

print_r(getLatLong('SW1W 9TQ'));
print_r(getLatLong('10011'));

This produces the following output.

Array
(
 [Latitude] => 51.489943
 [Longitude] => -0.154065
)
Array
(
 [Latitude] => 40.746497
 [Longitude] => -74.009447
)

I have tried this with UK and USA postal codes, but it would be interesting to see if it works with any other codes. Also, the query currently looks at google.co.uk, but that is only because I am based in the UK. You should change this to the nearest Google domain, so if you are based in the US then change this to google.com.

Comments

Add new comment

Post navigation

";
 echo "Longitude: ".$val['lng']."
"; ?>

Post navigation

You are here: Home / HTML / Get Latitude and Longitude from ZIP Code using Google Maps API and PHP

Today We are going to learn “Get Latitude and Longitude from ZIP Code using Google Maps API and PHP” this tutorial help in many project, i.e getting location on users address ZIP code.

For Pointing the exact location on Google Map it will always be a good idea to pass the latitude and longitude with Google Maps API. In this tutorial, you’ll know how to get latitude and longitude from ZIP code using Google Maps API in PHP.

Get Location

';
    echo "Longitude :" .$lng;
}
?>

Front End Design



	
		Location|techJunkGigs
	
	
		

Combine Code For Getting Longitude and Latitude



	
		Location|techJunkGigs
	
	
		
'; echo "Longitude :" .$lng; } ?>

Demo Image

How can i get latitude and longitude from zip code in php?

How can i get latitude and longitude from zip code in php?

So that’s all for this “Get Latitude and Longitude from ZIP Code using Google Maps API and PHP”  tutorial friends. We learned how to run get longitude and latitude with ZIP code . if you found it helpful then, please SHARE. Thank You

How can I get latitude and longitude from PHP pincode?

php $zipcode="92604"; $url = "http://maps.googleapis.com/maps/api/geocode/json?address=".$zipcode."&sensor=false"; $details=file_get_contents($url); $result = json_decode($details,true); $lat=$result['results'][0]['geometry']['location']['lat']; $lng=$result['results'][0]['geometry']['location']['lng']; echo "Latitude ...

How can I get latitude and longitude from address in PHP?

Function Usage: php $address = 'Brooklyn, NY, USA'; $address = str_replace(' ', '+', $address); $result = getGeoCode($address); echo 'Latitude: ' . $result['lat'] . ', Longitude: ' . $result['lng']; // produces output // Latitude: 40.6781784, Longitude: -73.9441579 ?>

How do I find the latitude and longitude of a zip code using Google API?

php if(isset($_POST['submit'])) { $zipcode=$_POST['location']; $url = "http://maps.googleapis.com/maps/api/geocode/json?address=".$zipcode."&sensor=false"; $details=file_get_contents($url); $result = json_decode($details,true); $lat=$result['results'][0]['geometry']['location']['lat']; $lng=$result['results'][0][' ...

How do I find the latitude and longitude of an address list?

Get the coordinates of a place.
On your computer, open Google Maps..
Right-click the place or area on the map. This will open a pop-up window. You can find your latitude and longitude in decimal format at the top..
To copy the coordinates automatically, left click on the latitude and longitude..