SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    access json data, can't get to a value

    Hello everyone!

    I am trying to get some value from a query to the Google maps reverse geolocation API and I get something like in the following link

    Data gotten from Google Maps API

    from there I can get some information using this

    PHP Code:
    foreach ( $data->results as $result ) {
        foreach ( 
    $result as $res ) {
            if(
    is_array($res)){
                if(
    is_object($res[0])){
                    switch (
    $res[0]->types[0]){
                        case 
    "street_number":
                            
    $street_number $res[0]->long_name;
                        break;
                        case 
    "route":
                            
    $route $res[0]->long_name;
                        break;
                        case 
    "neighborhood":
                            
    $neighborhood $res[0]->long_name;
                        break;
                        case 
    "locality":
                            
    $city $res[0]->long_name;
                        break;
                        case 
    "administrative_area_level_1":
                            
    $state $res[0]->long_name;
                        break;
                        case 
    "postal_code":
                            
    $postal_code $res[0]->long_name;
                        break;
                        case 
    "country":
                            
    $country $res[0]->long_name;
                        break;                    
                    }
                }
            }
        }
    }
    echo 
    '{"country":"' $country '","city":"' $city '","postal_code":"' $postal_code '","street_number":"' $street_number '","route":"' $route '","neighborhood":"' $neighborhood '","state":"' $state '","url":"' $url '"}'
    For that address provided this is the data I get in response after the PHP script

    PHP Code:
    {
    "country":"Mexico",
    "city":"",
    "postal_code":"",
    "street_number":"",
    "route":"Sin Número No. 3",
    "neighborhood":"",
    "state":"Oaxaca",
    "url":"https://maps.googleapis.com/maps/api/geocode/json?latlng=17.185264431680135,-96.76821982574461&sensor=true"

    I am trying to get the values of the address like street, street number, postal code, state country etc and that does work but for some reason it will only get some data, the structure of the data is the same for each value so I was under the assumption I could get every detail the same way but as you can see the city, street number, postal code and neighborhood are missing, from all those values if you look at the link I posted above street number and postal code are missing so that is a correct response but city and neighborhood are there and I am not getting them, can someone tell me what I'm doing wrong!
    Do you get bothered because I do the same thing every day?
    Do you question why I do it?
    Then find something that you actually like doing!!!

    Stop thinking on what I do.

  2. #2
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Okay well, I finally got it, this is the way I did it

    PHP Code:
    for ( $i 0$i count $data->results[0]->address_components ); $i ++ ) {
        switch ( 
    $data->results[0]->address_components[$i]->types[0] ) {
            case 
    "street_number":
                
    $street_number $data->results[0]->address_components[$i]->long_name;
            break;
            case 
    "route":
                
    $route $data->results[0]->address_components[$i]->long_name;
            break;
            case 
    "neighborhood":
                
    $neighborhood $data->results[0]->address_components[$i]->long_name;
            break;
            case 
    "locality":
                
    $city $data->results[0]->address_components[$i]->long_name;
            break;
            case 
    "administrative_area_level_1":
                
    $state $data->results[0]->address_components[$i]->long_name;
            break;
            case 
    "postal_code":
                
    $postal_code $data->results[0]->address_components[$i]->long_name;
            break;
            case 
    "country":
                
    $country $data->results[0]->address_components[$i]->long_name;
            break;
        }

    Do you get bothered because I do the same thing every day?
    Do you question why I do it?
    Then find something that you actually like doing!!!

    Stop thinking on what I do.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •