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 Latitude And Longitude From Address Using PHP And Google Maps API?

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

Hi! In this tutorial, let’s see how to get latitude and longitude from addresses using google maps geocoding API and PHP. Geocoding is the process of converting physical addresses into geographical coordinates such as latitude and longitude so you can use them to create markers on Google Maps. It is also better to provide latitude and longitude when using Google Map API so that you can point to an accurate position on the map. To obtain the geocode, we must send an HTTP request to the Google Map Geocoding API along with the formatted address.

To obtain physical Lat, Lng from the API, you have to send an HTTP request along with address 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.

Table of Contents


  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.

PHP Function To Get Address From Latitude And Longitude:

Below is the PHP function to get the address details of the given geographic coordinates. You must pass longitude and latitude to the function which makes an HTTP request to the API, receive a JSON response, and parse and return the formatted address.

function getLatLngFromGoogle($address) {
//Google Map API URL
$url = "".$address."&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);
$returnBody = json_decode($response);
// Google MAP
$status = $returnBody->status;
if($status == "REQUEST_DENIED"){ 
$result = $returnBody->error_message;
} else { 
$data['lat'] = $returnBody->results[0]->geometry->location->lat;
$data['lng'] = $json->results[0]->geometry->location->lng;
return $data; 


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

// coordinates
$address= 'Brooklyn, NY, USA'; 
$result = getAddressFromGoogle($address); 
echo 'Latitude: ' . $result['lat'] . ', Longitude: ' . $result['lng'];
// produces output
// Latitude: 40.6781784, Longitude: -73.9441579

That explains about getting the location from an address 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.

Recommended For You:
How To Change CSS Class Of Multiple DIV Having Same Classes?


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.

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 *