• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / PHP / Get Current Location Latitude Longitude in php

Get Current Location Latitude Longitude in php

April 4, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  Exploring Trigonometric Functions in PHP
  • 2.  In PHP isset Method is Used For Checking if Variable is Defined
  • 3.  php preg_match – Find Pattern in String Using Regex (Regular Expression)
  • 4.  How to Check If String Contains Substring in PHP
  • 5.  How to Get Session ID in php with session_id()
  • 6.  php Print Array – How to Print All Elements in Array
  • 7.  php getimagesize – Get Height, Width and Image File Type
  • 8.  Get First Element of Array in php
  • 9.  Get Query String from URL in php
  • 10.  php e – Using M_E and exp() Function to Get Euler’s Constant e

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · The Programming Expert · About · Privacy Policy