A Very Simple Script to get the latitude and longitude of a location using Google Map API!.
You might have seen many sites giving a tutorials which really confuse you a lot!
NOTE: Demo and Download link is at the bottom of the post!
Here we are going to use a pure and easy way of doing it using PHP Script!
Below is the Google Map API we are going to use!. Copy and paste this URL in the browser and you can see the result immediately.
https://maps.googleapis.com/maps/api/geocode/json?address=whitefield+bangalore&sensor=false
I think you got an idea now. It seems very simple right?.
Thinking about parsing the JSON result?. Don’t worry here is the entire code!
<html> <head><title>Simple PHP Script to get the Latitude and Longitute using Google API - Blog.Theonlytutorials.com</title></head> <body> <h2>Simple PHP Script to get the Latitude and Longitude using Google API - Blog.Theonlytutorials.com</h2> <br /> <form action="" method="post"> Type Your Location : <input type="text" name="myaddress" /> (eg. Whitefield, Bangalore) </form> <br /> <?php //blog.theonlytutorials.com //author: agurchand if(isset($_POST['myaddress'])){ $myaddress = urlencode($_POST['myaddress']); //here is the google api url $url = "https://maps.googleapis.com/maps/api/geocode/json?address=$myaddress&sensor=false"; //get the content from the api using file_get_contents $getmap = file_get_contents($url); //the result is in json format. To decode it use json_decode $googlemap = json_decode($getmap); //get the latitute, longitude from the json result by doing a for loop foreach($googlemap->results as $res){ $address = $res->geometry; $latlng = $address->location; $formattedaddress = $res->formatted_address; ?> <br /> <!-- Print the Latitude and Longitude --> <?php echo "Latitude: ". $latlng->lat ."<br />". "Longitude:". $latlng->lng; ?> <h2>Dynamic Location on Google Map!</h2> <iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=<?php echo $myaddress;?>&ie=UTF8&hq=&hnear=<?php echo urlencode($formattedaddress);?>&t=m&z=14&iwloc=A&output=embed"></iframe> <?php } } ?> </body> </html>
very useful coding ,really helpful
This is really nice…. thanks a lot… ๐
This is really nice…. thanks for this… ๐
Thanks
thank u somuch dear
is there any usage limit for using this api???
Yes, All the API has its limit. You can read the article here, https://developers.google.com/maps/documentation/business/articles/usage_limits#basics
how can i detect my current location automatically
Hi Aziman, Here is the code, https://blog.theonlytutorials.com/user-current-location-ip-address-simple-php-script/
very useful..amazing..
This is really niceโฆ. thanks for thisโฆ ๐
gooooooood……!!!
Very Nice Codding i am very glad to see this . thanks admin keep it up
Great !
very goooood code and short code nice work
Thanks for your post
is it any information how to determine our current location, no need to input the address but google map can directly determine where’s our location?
I’m using HTML5 geolocation but the accuracy wasn’t so good
any comment will be appreciated
Thanks!
Hi, You can use this script to find the current location:
https://blog.theonlytutorials.com/user-current-location-ip-address-simple-php-script/
not working for me
I am getting below warnings
Warning: file_get_contents(https://maps.googleapis.com/maps/api/geocode/json?address=balewadi+stadium%2C+pune&sensor=false) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in D:xampphtdocsgoogle_mapindex.php on line 19
Warning: Invalid argument supplied for foreach() in D:xampphtdocsgoogle_mapindex.php on line 23
did I missed some thing?
You need to change allow_url_fopen = 1 in php.ini
Nice Work. Thanks ๐
Awesome. it works fine
Thanks Friend !!! Keep doing such kind of development !!!
How I can extract postcode (zip) corresponding to an address?
What would be the script code?
many Thanks
Yes, its possible. Check this link in a browser: https://maps.googleapis.com/maps/api/geocode/json?address=madurai,sammatipuram&sensor=false
You can see a field called ‘postal_code’ in that. But I’m afraid you won’t get this for all the location!
THX!!!
Hello Agurchand Babu,
Usefull article.. Bookmarked ua website!
How to retrieve GPS location directly on website?? My GPS sends LANG & LAT but how can i integrate that to your current code??
Thanks boss it help a lot.
it’s working fine but when i am showing multiple location in for-each it’s showing first location only can you please help me how to do that.
Have you checked it in my demo how it is working?