PHP Code To Find The Street Address Given Latitude & Longitude Coordinates

You can use the Google Maps Geocoding API to retrieve the street address given a pair of latitude and longitude coordinates. Here’s a sample PHP code to achieve this:

<?php
// Replace YOUR_API_KEY with your Google Maps API key
$apiKey = 'YOUR_API_KEY';

// Latitude and Longitude of the location
$lat = '37.4224764';
$lng = '-122.0842499';

// Google Maps Geocoding API endpoint
$url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$lng&key=$apiKey";

// Make a GET request to the API endpoint
$response = file_get_contents($url);

// Decode the JSON response
$data = json_decode($response);

// Get the formatted address from the API response
$address = $data->results[0]->formatted_address;

echo $address;
?>

In the above code, replace YOUR_API_KEY with your actual Google Maps API key. Also, replace the $lat and $lng variables with the actual latitude and longitude coordinates that you want to find the street address for. The $address variable will contain the formatted street address as returned by the API.

Leave a Reply

Proudly powered by WordPress | Theme: Code Blog by Crimson Themes.