Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Latitude Longitude to Address

I have a form on my website where a user enters an address of a place. When they submit the form, I convert this location into latitude/longitude and store this in a MySQL Database. I am using Google's Geocode service for this conversion. The problem is that I can't find either a class or a service to convert that latitude/longitude back to an address and as far as I know, Google's Geocode service is a unidirectional conversion. I realize I could store the physical address in the database, but over time this is wasted space when it could be stored in a simpler format. Does anybody know of either a class/service to convert from latitude/longitude to a address or if I am wrong and I can use Google's geocode system? I have looked for the answer for the past few days but couldn't find anything. Thanks for your help!

like image 295
Joe Torraca Avatar asked Oct 10 '12 01:10

Joe Torraca


People also ask

How can I get full address from latitude and longitude in PHP?

Pass latitude and longitude in getaddress() function. It will return address string on success otherwise return boolean false. <? php $lat= 26.754347; //latitude $lng= 81.001640; //longitude $address= getaddress($lat,$lng); if($address) { echo $address; } else { echo "Not found"; } ?>

How can I get current location in PHP?

The getCurrentPosition() method is used to get the visitor's position and showLocation() method is used to getting the visitor's address from the getLocation. php file using Ajax. HTML Code: After getting the visitor position, the address will be shown on the web page ( #location span).


2 Answers

Conversion of geo-coordinates into address is known as Reverse Geocoding. In this script we are using Google map API because it’s free, fast and no API key required.

Rate Limit of Google reveres Geocoding is 2500 API calls per IP per day.

PHP function for Reveres Geocoding

<?
  function getaddress($lat,$lng)
  {
     $url = 'https://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($lat).','.trim($lng).'&sensor=false';
     $json = @file_get_contents($url);
     $data=json_decode($json);
     $status = $data->status;
     if($status=="OK")
     {
       return $data->results[0]->formatted_address;
     }
     else
     {
       return false;
     }
  }
?>

Pass latitude and longitude in getaddress() function. It will return address string on success otherwise return boolean false.

Example

<?php
  $lat= 26.754347; //latitude
  $lng= 81.001640; //longitude
  $address= getaddress($lat,$lng);
  if($address)
  {
    echo $address;
  }
  else
  {
    echo "Not found";
  }
?>
like image 137
Andre Hoffmann Avatar answered Oct 20 '22 08:10

Andre Hoffmann


<?php

/* 
* Given longitude and latitude in North America, return the address using The Google Geocoding API V3
*
*/

function Get_Address_From_Google_Maps($lat, $lon) {

$url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$lon&sensor=false";

// Make the HTTP request
$data = @file_get_contents($url);
// Parse the json response
$jsondata = json_decode($data,true);

// If the json data is invalid, return empty array
if (!check_status($jsondata))   return array();

$address = array(
    'country' => google_getCountry($jsondata),
    'province' => google_getProvince($jsondata),
    'city' => google_getCity($jsondata),
    'street' => google_getStreet($jsondata),
    'postal_code' => google_getPostalCode($jsondata),
    'country_code' => google_getCountryCode($jsondata),
    'formatted_address' => google_getAddress($jsondata),
);

return $address;
}

/* 
* Check if the json data from Google Geo is valid 
*/

function check_status($jsondata) {
    if ($jsondata["status"] == "OK") return true;
    return false;
}

/*
* Given Google Geocode json, return the value in the specified element of the array
*/

function google_getCountry($jsondata) {
    return Find_Long_Name_Given_Type("country", $jsondata["results"][0]["address_components"]);
}
function google_getProvince($jsondata) {
    return Find_Long_Name_Given_Type("administrative_area_level_1", $jsondata["results"][0]["address_components"], true);
}
function google_getCity($jsondata) {
    return Find_Long_Name_Given_Type("locality", $jsondata["results"][0]["address_components"]);
}
function google_getStreet($jsondata) {
    return Find_Long_Name_Given_Type("street_number", $jsondata["results"][0]["address_components"]) . ' ' . Find_Long_Name_Given_Type("route", $jsondata["results"][0]["address_components"]);
}
function google_getPostalCode($jsondata) {
    return Find_Long_Name_Given_Type("postal_code", $jsondata["results"][0]["address_components"]);
}
function google_getCountryCode($jsondata) {
    return Find_Long_Name_Given_Type("country", $jsondata["results"][0]["address_components"], true);
}
function google_getAddress($jsondata) {
    return $jsondata["results"][0]["formatted_address"];
}

/*
* Searching in Google Geo json, return the long name given the type. 
* (If short_name is true, return short name)
*/

function Find_Long_Name_Given_Type($type, $array, $short_name = false) {
    foreach( $array as $value) {
        if (in_array($type, $value["types"])) {
            if ($short_name)    
                return $value["short_name"];
            return $value["long_name"];
        }
    }
}

/*
*  Print an array
*/

function d($a) {
    echo "<pre>";
    print_r($a);
    echo "</pre>";
}
like image 35
Moh Avatar answered Oct 20 '22 09:10

Moh