1

I was wondering if someone could help me out with a bit of code I have.

I have a function which is as follows:

function getAddressComponent($lat, $lng, $field) {

    $returnValue = '-';
    $url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($lat).','.trim($lng).'&sensor=false';
    $data = @file_get_contents($url);
    $json = json_decode($data, TRUE);

    if (isset($json['results'])) {

        foreach($json['results'] as $result) {

            foreach ($result['address_components'] as $address_component) {

                $types = $address_component['types'];
                if (in_array($field, $types) && sizeof($types) == 1) {

                    $returnValue = $address_component['short_name'];

                }
            }
        }
    }

    return $returnValue;

}

I call the function like this:

$returnAddress = $this->getAddressComponent( -34.872693, 138.490391, 'administrative_area_level_1' );

If i run the function for the administrative_area_level_1 type, it returns nothing, but if i use the $url and enter the lat and lng in the url it returns results for that type... The array looks like so.

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "22",
               "short_name" : "22",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Blue-Sails Court",
               "short_name" : "Blue-Sails Ct",
               "types" : [ "route" ]
            },
            {
               "long_name" : "West Lakes",
               "short_name" : "West Lakes",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "South Australia",
               "short_name" : "SA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Australia",
               "short_name" : "AU",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "5021",
               "short_name" : "5021",
               "types" : [ "postal_code" ]
            }
         ],

It returns the '-' for the locality and country one as well.

I want it to return a '-' if it doesnt exist, but in this case it does exist and its doing my head in.

Any help would be greatly appreciated.

Cheers,

1 Answer 1

3

The biggest problem is the condition inside the if:

As you can see in this json that you provided:

"types" : [ "administrative_area_level_1", "political" ]

Its sizeof/count is greater than zero, it contains 2.

So when you use the condition:

if (in_array($field, $types) && sizeof($types) == 1) { // this fails
// sizeof is not equal to 1

So basically, no values are being assigned inside $returnValue, thus it just returns your initialization -.

Either you just change it to > 0 or just remove it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.