Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to get Latitude/longitude from address in PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 245
    Comment on it

    Hello All,

    In this blog we will discuss about how to fetch the lat/long from the given address in PHP.

    So we will be using curl request for fetching the same via using Google maps api.


    Below is the code, you can use to generate lat/long :

    public function getLatLng($add=null)
        {
            $result_val = '';
            $response = array();
            if(!empty($add))
            { 
                $address = str_replace(" ", ",+", $add);
            }
            else
            {
                $address = '+Illinois,United+States';
            }
    
            $strAdd = "http://maps.googleapis.com/maps/api/geocode/json?address=" . $address . "&sensor=false";
            $ch = curl_init($strAdd);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_POST, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $output = curl_exec($ch);
            curl_close($ch);
            $result = json_decode($output, true);
            $result_val = $result['results'][0]['geometry']['location'];
            $lat_val = $result['results'][0]['geometry']['location']['lat'];
            $lng_val = $result['results'][0]['geometry']['location']['lng'];
    
        $response['lat'] = $lat_val;
        $response['long'] = $lng_val;
    
        return $response;
        }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: