How get lat long from address google maps api 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

46k8 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,10311 silver badges22 bronze badges

answered Dec 3, 2014 at 6:03

1

How do I get the lat long from an address in Google Maps API?

get lat long from address google api.
function getCoordinates[address]{.
fetch["//maps.googleapis.com/maps/api/geocode/json? address="+address+'&key='+API_KEY].
. then[response => response. ... .
. then[data => {.
const latitude = data. results[0]. ... .
const longitude = data. results[0]. ... .
console. log[{latitude, longitude}].

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.

How do I pull coordinates from Google Maps?

Right-click the map location. Copy the GPS coordinates in the pop-up window. Android app: In Google Maps app, press and hold a location to drop a red pin. Copy the coordinates in the search box at the top of the screen.

Chủ Đề