PHP

PHP Latitude Longitude To Address

24 April, 2021 1 minute read

PHP Latitude Longitude to Address

Geolocation PHP convert Latitude Longitude to Address. First you need to get the api key for google map api that you find using below link:

https://developers.google.com/maps/documentation/geocoding/start#ReverseGeocoding

After put the below function into your helper class and call that function anywhere you want simple passing lat and long. After passing lat long they return address regarding lat long values. That whole process called Reverse Geocoding.

/**
 * find address using lat long
 */
public static function geolocationaddress($lat, $long)
{
    $geocode = "https://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$long&sensor=false&key=AIzaSyCJyDp4TLGUigRfo4YN46dXcWOPRqLD0gQ";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $geocode);
    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);
    $output = json_decode($response);
    $dataarray = get_object_vars($output);
    if ($dataarray['status'] != 'ZERO_RESULTS' && $dataarray['status'] != 'INVALID_REQUEST') {
        if (isset($dataarray['results'][0]->formatted_address)) {

            $address = $dataarray['results'][0]->formatted_address;

        } else {
            $address = 'Not Found';

        }
    } else {
        $address = 'Not Found';
    }

    return $address;
}
Vishnu Sharma

Vishnu Sharma

Full Stack Developer at Signifier Technology Pvt Ltd

Leave a Reply

More from Rannkly

Our best blog on PHP

PHP

Integrate CCavenue Payment Gateway In PH...

Shobhit Singh

Shobhit Singh

10 minute read

Integrating CCAvenue with php web application

Read more

Laravel Latest Version 5.7 With Material...

Stuti varshney

Stuti varshney

1 minute read

Read more
PHP

Sending Mobile Verification OTP SMS Usin...

Shobhit Singh

Shobhit Singh

2 minute read

Read more