PHP Script to get the Latitude and Longitude using Google Map API – Simple Script!!

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&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=<?php echo $myaddress;?>&amp;ie=UTF8&amp;hq=&amp;hnear=<?php echo urlencode($formattedaddress);?>&amp;t=m&amp;z=14&amp;iwloc=A&amp;output=embed"></iframe>
<?php
}
}
?>
</body>
</html>

 

DemoDownload

 

29 thoughts on “PHP Script to get the Latitude and Longitude using Google Map API – Simple Script!!

  1. 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!

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

  3. 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.

Leave a Reply

Theme: Overlay by Kaira
Agurchand