How can i get latitude and longitude from address in php?

I have used the autocomplete of geocode but when I am selecting from the drop down it is giving me the lat and long of the address

I need to check when the lat and long are empty then from the posted address I must get the latitude and longitude

lagbox

45.9k8 gold badges67 silver badges83 bronze badges

asked Apr 22, 2014 at 7:02

2

Suppose you have hidden value for lat and long is mapLat & mapLong and input field name is location then:












extract[$_POST];
if[$mapLat =='' && $mapLong =='']{
        // Get lat long from google
        $latlong    =   get_lat_long[$location]; // create a function with the name "get_lat_long" given as below
        $map        =   explode[',' ,$latlong];
        $mapLat         =   $map[0];
        $mapLong    =   $map[1];    
}


// function to get  the address
function get_lat_long[$address]{

    $address = str_replace[" ", "+", $address];

    $json = file_get_contents["//maps.google.com/maps/api/geocode/json?address=$address&sensor=false®ion=$region"];
    $json = json_decode[$json];

    $lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
    $long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
    return $lat.','.$long;
}

answered Apr 22, 2014 at 7:09

6

Using CURL


answered Apr 1, 2017 at 9:27

Mukesh ChapagainMukesh Chapagain

24.3k15 gold badges114 silver badges117 bronze badges

2

I tried above solutions but none was working then i tried to fix on my own so below is the correct code:

    $going=$this->input->post['going'];

    $address =$going; // Google HQ
    $prepAddr = str_replace[' ','+',$address];
    $apiKey = 'Add your API Key'; // Google maps now requires an API key.

    $geocode=file_get_contents['//maps.googleapis.com/maps/api/geocode/json? 
   address='.urlencode[$address].'&sensor=false&key='.$apiKey];

    //print_r[$geocode];

    $output= json_decode[$geocode];
    $latitude = $output->results[0]->geometry->location->lat;
    $longitude = $output->results[0]->geometry->location->lng;

answered Oct 10, 2019 at 17:39

FahadKhanFahadKhan

711 silver badge3 bronze badges

2

// We define our address
$address = 'Indore, MP 452001';
echo"
";
print_r[get_lat_long[$address]];

// function to get  the address
function get_lat_long[$address] {
   $array = array[];
   $geo = file_get_contents['//maps.googleapis.com/maps/api/geocode/json?address='.urlencode[$address].'&sensor=false'];

   // We convert the JSON to an array
   $geo = json_decode[$geo, true];

   // If everything is cool
   if [$geo['status'] = 'OK'] {
      $latitude = $geo['results'][0]['geometry']['location']['lat'];
      $longitude = $geo['results'][0]['geometry']['location']['lng'];
      $array = array['lat'=> $latitude ,'lng'=>$longitude];
   }

   return $array;
}

Till Helge

9,1072 gold badges39 silver badges56 bronze badges

answered Jul 13, 2016 at 7:49

1

Try this for getting the address:

xxx

1,09711 silver badges22 bronze badges

answered Dec 3, 2014 at 6:03

1

How can I get the latitude and longitude from address in php?

how to get current location latitude and longitude in php.
function geoLocate[$address].
$lat = 0;.
$lng = 0;.
$data_location = "//maps.google.com/maps/api/geocode/json? key=".$ GOOGLE_API_KEY_HERE." &address=". str_replace[" ", "+", $address]." &sensor=false";.
$data = file_get_contents[$data_location];.

How do I find a latitude and longitude for an address?

Get the coordinates of a place.
On your computer, open Google Maps..
Right-click the place or area on the map. This will open a pop-up window. You can find your latitude and longitude in decimal format at the top..
To copy the coordinates automatically, left click on the latitude and longitude..

How get lat long from address Google Maps API php?

function getaddress[$lat, $lng] { $url = '//maps.googleapis.com/maps/api/geocode/json?latlng=' . trim[$lat] . ',' .

How can I get latitude and longitude from php pincode?

php $zipcode="92604"; $url = "//maps.googleapis.com/maps/api/geocode/json?address=".$zipcode."&sensor=false"; $details=file_get_contents[$url]; $result = json_decode[$details,true]; $lat=$result['results'][0]['geometry']['location']['lat']; $lng=$result['results'][0]['geometry']['location']['lng']; echo "Latitude ...

Chủ Đề