• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
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.  Get Word Count of String in php with str_word_count() Function
  • 2.  Get Array Length with php count() Function
  • 3.  How to Check If String Contains Substring in PHP
  • 4.  How to Reverse Array in php Without a Function or Other Variables
  • 5.  preg_replace php – Use Regex to Search and Replace
  • 6.  How to Parse URLs with php parse_url() Function
  • 7.  Remove Last Character from String Using php
  • 8.  php trim() Function – Remove Whitespace at Beginning and End of String
  • 9.  How to Get Current System IP Address in php
  • 10.  Creating Custom category.php With Pagination

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 *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

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

x