MrK
MrK

Reputation: 1078

Google Geo API (Latitude + Longitude)

I am currently trying to solve my users location from the GEO I get with html5, now im trying to fetch the formatted address from the Google API.

I have done this:

function LatLong($Latitude, $Longitude) {


    $Url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=$Latitude,$Longitude&sensor=true";


    //send request:
    $Client = curl_init($Url);

    //Set options:
    curl_setopt($Client, CURLOPT_RETURNTRANSFER, 1);

    //query the API and fetch results:
    $Response = curl_exec($Client);

    //decode the response:
    return  json_decode($Response);

}

Now, to get the full result, i tried this just to see if it worked:

print_r(LatLong($_COOKIE['latitude'], $_COOKIE['longitude'])->results);

I do get a result:

"results" : [
  {
     "address_components" : [
        {
           "long_name" : "4",
           "short_name" : "4",
           "types" : [ "street_number" ]
        },
        {
           "long_name" : "Neuwerk - Cuxhaven",
           "short_name" : "Neuwerk - Cuxhaven",
           "types" : [ "route" ]
        },
        {
           "long_name" : "Hamburg-Insel Neuwerk",
           "short_name" : "Hamburg-Insel Neuwerk",
           "types" : [ "sublocality_level_1", "sublocality", "political" ]
        },
        {
           "long_name" : "Hamburg",
           "short_name" : "Hamburg",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Hamburg",
           "short_name" : "Hamburg",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "Tyskland",
           "short_name" : "DE",
           "types" : [ "country", "political" ]
        },
        {
           "long_name" : "27499",
           "short_name" : "27499",
           "types" : [ "postal_code" ]
        }
     ],
     "formatted_address" : "Neuwerk - Cuxhaven 4, 27499 Hamburg, Tyskland",
     "geometry" : {
        "location" : {
           "lat" : 53.91403529999999,
           "lng" : 8.4899886
        },
        "location_type" : "ROOFTOP",
        "viewport" : {
           "northeast" : {
              "lat" : 53.91538428029149,
              "lng" : 8.491337580291502
           },
           "southwest" : {
              "lat" : 53.91268631970849,
              "lng" : 8.488639619708497
           }
        }
     },
     "place_id" : "ChIJrT-2jXcZtEcRv91jn7XQhcQ",
     "types" : [ "street_address" ]
  }

I want to print formatted_address and i tried to do it like this:

print_r(LatLong($_COOKIE['latitude'], $_COOKIE['longitude'])->results->formatted_address);

But I don't get anything, what did I do wrong ?

Upvotes: 2

Views: 202

Answers (2)

FelisCatus
FelisCatus

Reputation: 5334

As posted in the question, results is an array. Which means you probably want to access the first element before taking formatted_address.

Try this:

print_r(LatLong($_COOKIE['latitude'], $_COOKIE['longitude'])->results[0]->formatted_address);

Upvotes: 2

Arunendra
Arunendra

Reputation: 570

 $string = file_get_contents('./string.json');
  $json = json_decode($string);

if you want to have items: :

foreach ($json['items'] as $address)
{
    echo "items:". $address['address'] ."\n";
};

anyway if you are not sure about how an array is built you could print it via:

print_r($json);

which will print:

Array ( [items] => Array ( [0] => Array ( [address] => W 7th Ave )

        [1] => Array
            (
                [address] => W 8th St
            )
    )

)

now you found out that $json contains just an array (items) of two array, then, if you loop it, you will get that array which is printed in your example. As explained above you need to go one step deeper by looping the elements in your items array and print their address element.

here is the complete script: http://pastie.org/2275879

Upvotes: 0

Related Questions