How to get Longitude and Latitude from an Address using PHP | Geocoding | JSON | Plot on Google Map using Location Address
anoopsachari | Apr 30, 2010 | Comments 34
In my previous article i explained “google-map-integration-using-javascript-php” . In all examples explained in the previous post we used longitude and latitude value to plot the location and we needed to find it manually . In many instance we would need to plot the location using an address . It can be done using a small php script .
$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address=Marine+Drive+GCDA+Complex+Cochin&sensor=false');
$output= json_decode($geocode);
$lat = $output->results[0]->geometry->location->lat;
$long = $output->results[0]->geometry->location->lng;
echo "Latitude : ".$lat;
echo "Longitude : ".$long;
The php code above makes a request to Google maps API using 2 arguments address and sensor . Each word in address should be joined using ‘+’ symbol as show above and the response we get is in json format which is decrypted to get latitude and longitude .
A complete example to plot on Google map using an Address you have .
You need to get a Google Map API to use this code . You can register here for a Google Map API key . Replace ‘ADD_YOUR_KEY_HERE’ with your KEY
<?php
$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address=Maharajas+College+Cochin&sensor=false');
$output= json_decode($geocode);
$latitude = $output->results[0]->geometry->location->lat;
$longitude = $output->results[0]->geometry->location->lng;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://maps.google.com/maps?file=api&v=1&key=ADD_YOUR_KEY_HERE" type="text/javascript"></script>
</head>
<body>
<div id="map" style="width: 400px; height: 300px"></div>
<script type="text/javascript">
var map = new GMap(document.getElementById("map"));
var point = new GPoint(<?php echo $longitude; ?>,<?php echo $latitude; ?>);
var address = 'Maharajas College | Cochin';
var mark = createInfoMarker(point, address);
map.addOverlay(mark);
function createInfoMarker(point, address) {
var marker = new GMarker(point);
map.centerAndZoom(point, 3);
GEvent.addListener(marker, "click",
function() {
marker.openInfoWindowHtml(address);
}
);
return marker;
}
</script>
</body>
</html>
The output of above code is
Hope this tutorial helped you ! Comment me if u need any help .
Filed Under: javascript • php
About the Author: a holistic web developer , movie buff and technical blogger from queen of arabian sea.








[...] « Credit Card Number Validation using PHP How to get Longitude and Latitude from an Address using PHP | Geocoding | JSON [...]
thanks for this…….it really helps a lot and made my work easier
great work……keep it up!!!!
So sorry I can’t find your name! So, hi. Wonder if you can help put some javascript mapping code into php? please email me with your email address so I can show you what I have, what it does, and what I would like to do with it.
Many thanks if you can.
@nsachc mail me @anoop.s.achari@gmail.com
i’ve just mailed you one problem,am a beginer to this developing field..kindly help me..
When i run this script on Fedora 10 linux LAMP it give warning
“Warning: file_get_contents() [function.file-get-contents]: php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution”
why?
@ankit . i never came across such a issue with the code . try updating your php version to latest version .
I’m using php 5.2.14. when i run on command line it works for me & display result as u shown above.
But when i access it through BROWSER it displays blank
Done Bro.
I have to enable “allow_url_include”.
Thank you Ankit for the new info .
good work bro keep it up
Hi, it is a good job. and I wonder if I can get the real of users latitude and longitude, because when i rus this I only see exactly the same as your sample.
Pherum
@pherum : you can see that in second line i have specified
“address=Maharajas+College+Cochin” the longitude and latitude values are calculated according to that , you may change it to the address you required so that google maps would plot them accordingly.
Hi anoopsachari,
Nice article.I am just a newbie to google maps.All i am planning is a custom google maps in which a user can log into a site.Then he is shown a text box to search location by zip code.The searched Zip code diameter is shown on the map.The user can click on any area there and mark that place,(like on the click,the exact long and lat is fetched) and the user can add the name and description to that place and all this details are stored in the mysql database and only a logged in user who created this mark should be able to see those marks and it should not be visible on publicly on google maps.
Please give a hint as how to start on this.
regards,
John
@john : the application you planning to do is a complicated application. i dont know weather google map API provide you permission to highlight a diameter using zipcode.
second part could be implemented using a mysql db. like logging the info [ longitude and latitude ] marked by the user. so that the next time he login he can view all places marked by him. But the question is . “how will you communicate with the local mysql db when a user marks on google maps ?”
Hi anoopsachari,
Thanks for the prompt answer.I need to know about a Google Maps API function that will give the correct [long and lat] when a user clicks on ANY WHERE on the map.As the user is logged(Mysql user registration and login) into to the map view,then only [long and lat] is collected and for that corresponding [long and lat] we will store the information in the DB.So again when user logins in all the db information for his marked [long and lat] is displayed.It will be using ajax with google map api
Any further idea on this?
Hi, long time lurker here , just wanted to say how I’m a big fan , but wish you would change your theme! You can get a free wordpress theme here, that’s where I got mine.
Świetnie to napisałeś
cieszę się, że wszedłem na tę stronkę, bo bardzo fajnie się czyta.
on prob of the plugin for conversion…..when i double click a text area for full selection ..the text converion menu is shown
This is so great I like this code. Thank’s man .
Nice, thank you for example
very helpfull for my new project:D
where’s the zoom in and zoom out?
Nice tutorial thanks buddy … its reallly a good one
Nice work. I very quickly was able to get this all up and running. Thank you so much for sharing!!!
Hey thank you for this help me to do my work more faster.
Hi… google does not understand postal codes? like e.g if i just use 90002 which is Los Angeles in USA it doesnt give right response. I need to use Los Angeles. Do i need to add anything else with the zip code or postal code to find the co-ordinates?
Thanks buddy, nice work. keep it up.
using php i have to make program which accepts two numbers (latitude and longitude) and converts it into a postal address.can u help that how to get this
Hi…really nice code…thanks. but where are the zoom in and zoom out buttons??
id – 1,
college – auburn,
zipcode – 36849,
url – http://www.auburn.edu,
ipeds is – 100858,
campus – city,
latitude – 32.59942,
logitude – -85.4881,
pls can you help me with a script for this… pulling from my database of a million rows of the value. and how i can walk it out with pictures given the url and lat. and long.
thank you
Hello achari,
i have to display more than one place in this code….is it possible????
missing ) after argument list
[Break On This Error] var map = new GMap(documentgetElementById(“map”));
getting this error can u pls reply on this…
Thanks
Hey very cool web site!! Man .. Beautiful .. Amazing .. I’ll bookmark your website and take the feeds also…I’m happy to find so many useful info here in the post, we need develop more strategies in this regard, thanks for sharing. . . . . .