PHP get latitude and longitude from address using Google geolocation API

How to get latitude and longitude from address using Google geolocation API PHP. Many times we need to get the latitude and longitude from the address given by the user. Let’s see the example of getting the latitude and longitude from address using Google geolocation API.

We send the request for getting the latitude and longitude using file_get_contents PHP function like.

$mapdata = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address=Rajkot%20Gujarat%20India');
$googlemapdata = json_decode($mapdata);

$lat = $googlemapdata->results[0]->geometry->location->lat;
$lng = $googlemapdata->results[0]->geometry->location->lng;

echo $lat;
echo '<br>';
echo $lng;

Wow! it’s too easy to get the latitude and longitude from address using PHP.

If you face any issue after using above code write the comment below.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s