LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / PHP Codes » How To Get Address From Latitude And Longitude Using PHP And Google Maps API?

How To Get Address From Latitude And Longitude Using PHP And Google Maps API?

How-To-Get-Address-From-Latitude-And-Longitude-Using-PHP-And-Google-Maps-API
Hi! Today let’s see how to get addresses from latitude and longitude using PHP and Google Maps Geocoding API. The process of converting geometric coordinates such as latitude and longitude into addresses is called reverse geocoding. And the geocoding is just the opposite, converting an address into latitude and longitude which we have seen in our previous tutorial. Google Maps provides a separate Geocoding API for the purpose and let’s see how to use it with PHP.

To obtain physical addresses from the API, you have to send an HTTP request along with latitude and longitude values.

There are many code snippets available online or on many other blogs and websites, but everyone cannot optimize your blog or website, so you need some optimized code snippets. So now checkout out the code snippet for your blog and website that will give you all features for your desired code. Now grab the ready-to-use code and paste it where you want.

Features:

  1. Light Weight.
  2. Pure PHP Code.
  3. Cross Browser.
  4. No External Files.
  5. Fully Customizable.
  6. Responsive.

Google API Return:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "58",
               "short_name" : "58",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Brooklyn Avenue",
               "short_name" : "Brooklyn Ave",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Crown Heights",
               "short_name" : "Crown Heights",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "long_name" : "Brooklyn",
               "short_name" : "Brooklyn",
               "types" : [ "political", "sublocality", "sublocality_level_1" ]
            },
            {
               "long_name" : "Kings County",
               "short_name" : "Kings County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "New York",
               "short_name" : "NY",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "11216",
               "short_name" : "11216",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "58 Brooklyn Ave, Brooklyn, NY 11216, USA",
         "geometry" : {
            "location" : {
               "lat" : 40.677978,
               "lng" : -73.94438700000001
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 40.67932698029149,
                  "lng" : -73.94303801970851
               },
               "southwest" : {
                  "lat" : 40.6766290197085,
                  "lng" : -73.94573598029152
               }
            }
         },
         "place_id" : "ChIJaVKlrIVbwokRhqlQjSdxUHc",
         "types" : [ "street_address" ]
      },
      
      ...
      ...
   ],
   "status" : "OK"
}

The response sends a status flag which you can test to determine the success or failure of the conversion process.

Recommended For You:
TikTok Style CSS Page Loader Animation With CSS3 Only

PHP Function To Get Address From Latitude And Longitude:

Below is the php function to get the address details of the given geographic co-ordinates. You must pass longitude and latitude to the function which makes a http request to the api, receive json response, parse and return the formatted address.

<?php
function getAddressFromGoogle($latitude,$longitude) {
//Google Map API URL
$API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; // Google Map Free API Key
$url = "https://maps.google.com/maps/api/geocode/json?latlng=".$latitude.",".$longitude."&key=".$API_KEY."";
// Send CURL Request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$returnBody = json_decode($response);
// Google MAP
$status = $returnBody->status;
if($status == "REQUEST_DENIED"){ 
$result = $returnBody->error_message;
} else { 
$result = $returnBody->results[0]->formatted_address;
}
return $result;
}
?>

Usage:

You have to access the above getAddressFromGoogle() function like this,

<?php
// coordinates
$latitude = '40.6781784';
$longitude = '-73.9441579';
$result = getAddressFromGoogle($latitude, $longitude);
echo 'Address: ' . $result;
// produces output
// Address: 58 Brooklyn Ave, Brooklyn, NY 11216, USA
?>

That explains about getting location from latitude and longitude in php using Google Maps Geocoding API. You can use this address info to place a marker on Google Maps. I hope you like this tutorial. Please share this post in your social circle if you find it useful.

Customization:

No need to customize it. Just copy-paste. Rest edit the code as per comments and need. Remember to add JavaScript after HTML code.

Troubleshooting the Errors:

Do it with concentration and patience. Check your all steps and again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Recommended For You:
How To Move Data/Variables From JavaScript To PHP And Vice Versa?

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us If you have any doubts or problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave.

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *