In php, we can get the current location’s latitude and longitude from an address and also get an address from a latitude and longitude by leveraging the Google Maps API.

To get the latitude and longitude from an address, you can define a custom php function as shown below.

function getLatLong($address){
    $formatted_address = str_replace(' ','+',$address);
    $geocodeFromAddr = file_get_contents('//maps.googleapis.com/maps/api/geocode/json?address='.$$formatted_address.'&sensor=false');
    $output = json_decode($geocodeFromAddr);
    $data['latitude'] = $output->results[0]->geometry->location->lat;
    $data['longitude'] = $output->results[0]->geometry->location->lng;
    if(!empty($data)){
        return $data;
    } else {
        return false;
    }
}

print_r(getLatLong("44 E 161st St, Bronx, NY 10451"));

//Output: 
Array
(
    [latitude] => 40.827519
    [longitude] => -73.925879
)

To go the other way and get an address given longitude and latitude coordinates, you can define a custom php function as shown below.

function getAddress($latitude,$longitude){
    $geocodeFromLatLong = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($latitude).','.trim($longitude).'&sensor=false'); 
    $output = json_decode($geocodeFromLatLong);
    $status = $output->status;
    
    $address = ($status=="OK") ? $output->results[1]->formatted_address:'';
    if(!empty($address)){
        return $address;
    }else{
        return false;
    }
}

echo getAddress();

// Output:
44 E 161st St, Bronx, NY 10451, USA

When designing forms for our web pages in php, many times we are asking for an address.

After receiving an address, the ability to get the longitude and latitude to determine the location of an address can be useful.

The Google Maps Geocoding API allows us to find the longitude and latitude given an address.

We can leverage the Google Maps Geocoding API and define a function which takes in an address and returns the longitude and latitude.

Below is an example of how to get the coordinates given an address in php.

function getLatLong($address){
    $formatted_address = str_replace(' ','+',$address);
    $geocodeFromAddr = file_get_contents('//maps.googleapis.com/maps/api/geocode/json?address='.$$formatted_address.'&sensor=false');
    $output = json_decode($geocodeFromAddr);
    $data['latitude'] = $output->results[0]->geometry->location->lat;
    $data['longitude'] = $output->results[0]->geometry->location->lng;
    if(!empty($data)){
        return $data;
    } else {
        return false;
    }
}

print_r(getLatLong("44 E 161st St, Bronx, NY 10451"));

//Output: 
Array
(
    [latitude] => 40.827519
    [longitude] => -73.925879
)

How to Get the Current Location Given Longitude and Latitude in php

If you’d like to go the other way, and get an address given the longitude and latitude, we can adjust our function from above.

We will still use the Google Maps Geocoding API, but now, instead we will pass the longitude and latitude instead of a formatted address.

Below is an example of how to get an address given longitude and latitude in php.

function getAddress($latitude,$longitude){
    $geocodeFromLatLong = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($latitude).','.trim($longitude).'&sensor=false'); 
    $output = json_decode($geocodeFromLatLong);
    $status = $output->status;
    
    $address = ($status=="OK") ? $output->results[1]->formatted_address:'';
    if(!empty($address)){
        return $address;
    }else{
        return false;
    }
}

echo getAddress();

// Output:
44 E 161st St, Bronx, NY 10451, USA

Hopefully this article has been useful for you to get the current location and get the longitude and latitude from an address in php.

Categorized in:

PHP,

Last Update: February 26, 2024