2

I would like parse data from google maps geocode version 3 through json. I would like to get details like locaityName, AdministrativeAreaName and status code. May I know how to parse these data? Thank you

1
  • Man, 2 years later and still no one answered your question directly. Commented Aug 7, 2012 at 18:56

4 Answers 4

3
function getGoogleAddressCoordinates()
    {
        //using the google maps api to get the long and lat coordinates of addresss
        //using form post variables
        $address =trim($_POST['address']);
        $city = trim($_POST['city']);
        $state = trim($_POST['state']);

        //formats the address add '+' into space
        $address = str_replace( ' ', '+' ,$address);
        $city = str_replace( ' ', '+', $city);
        $address = preg_replace("[^A-Za-z0-9]", '+', $address ); //remove non alpha numeric chars such as extra spaces.
        $address_str = $address.',+'. $city.',+'.$state;
        $geo_url = "http://maps.google.com/maps/api/geocode/json?address=$address_str&sensor=false";
        //echo $geo_url;
        //echo file_get_contents($geo_url);
        $json_result = json_decode(file_get_contents($geo_url));

        $geo_result =  $json_result->results[0];
        $coordinates = $geo_result->geometry->location;

        //see if the it is locating the real address or just apox location, or in most cases a bad address
        if($geo_result->types[0] == 'street_address')
            $coordinates->valid_address  = TRUE;
        else
            $coordinates->valid_address  = FALSE;

        return $coordinates;
    }
Sign up to request clarification or add additional context in comments.

Comments

0

PHP has json_decode() that will convert a JSON data stream into a PHP array or object.

2 Comments

I would like to parse specific data from google geocode as I mentioned above. do you know the structure of parsing these data? Thanks.
@ben why don't you just make a request and find out yourself using print_r()? It's the easiest way.
0

You can use the json_encode and json_decode functions to parse json data.

More info:

http://php.net/manual/en/function.json-encode.php

1 Comment

I would like to parse specific data from google geocode as I mentioned above. do you know the structure of parsing these data? Thanks.
0

Using the PHP PECL json library

json_decode()

http://php.net/manual/en/function.json-encode.php

Just an advice, if the function doesn't return anything that means that there is a syntax error into the JSON, it will not raise any error or log.

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.