The following PHP function will generate Latitude and Longitude from given zip code.
function getLnt[$zip]{ $url = "//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:
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 = "//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
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
Front End Design
Location|techJunkGigs Enter Your ZIP Code
Combine Code For Getting Longitude and Latitude
Location|techJunkGigs Enter Your ZIP Code
Demo Image
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