Google trends Public Holidays Coupon Code Code Compiler

Get Latitude and Longitude from address using Google Maps API using PHP


Dec 25, 2018

For Pointing the exact location on Google Map it will always be a good idea to pass the lat and long with Google Maps API. You’ll know how to get latitude and longitude from address using Google Maps API in PHP. Here we’ll combine all code into a PHP and you only need to use this to find latlong from an address.

GetLatitudeLongitude() function accept one parameter ($address), into the $address you need to provide the full address from where you want to get latitude and longitude. This function returns the latitude and longitude of address as an array if found, otherwise, return FALSE.

$address = '1600 Pennsylvania Ave NW, Washington, DC 20500, USA';


function GetLatitudeLongitude($address){
    if(!empty($address)){
        //Formatted address
        $Address= str_replace(' ','+',$address);
        //Send request and receive json data by address
        $getGeocodeAddr = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.$Address.'&sensor=false$key=GOOGLEAPIKEY'); 
        $result = json_decode($getGeocodeAddr);
        //Get latitude and longitute from json data
        $data['latitude']  = $result->results[0]->geometry->location->lat; 
        $data['longitude'] = $result->results[0]->geometry->location->lng;
        //Return latitude and longitude of the given address
        if(!empty($data)){
            return $data;
        }else{
            return false;
        }
    }else{
        return false;   
    }
}

Use GetLatitudeLongitude() Function Like The Following.


 $latL = $latLong['latitude']?$latLong['latitude']:'null';$l

Copyright 2024. All rights are reserved