简体   繁体   中英

how do i get a value from simplexml_load_file?

Hello i need to print the long and lat values from Google Geocode. I have retrieved the xml using:

$url ="http://maps.googleapis.com/maps/api/geocode/xml?address=se18lu&sensor=false";
$result = simplexml_load_file($url);

and it returns the following:

SimpleXMLElement Object
(
    [status] => OK
    [result] => SimpleXMLElement Object
        (
            [type] => postal_code
            [formatted_address] => Bankside, London Borough of Lambeth, London SE1 8LU, UK
            [address_component] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [long_name] => SE1 8LU
                            [short_name] => SE1 8LU
                            [type] => postal_code
                        )

                    [1] => SimpleXMLElement Object
                        (
                            [long_name] => Bankside
                            [short_name] => Bankside
                            [type] => Array
                                (
                                    [0] => sublocality
                                    [1] => political
                                )

                        )

                    [2] => SimpleXMLElement Object
                        (
                            [long_name] => London
                            [short_name] => London
                            [type] => Array
                                (
                                    [0] => locality
                                    [1] => political
                                )

                        )

                    [3] => SimpleXMLElement Object
                        (
                            [long_name] => London Borough of Lambeth
                            [short_name] => London Borough of Lambeth
                            [type] => Array
                                (
                                    [0] => administrative_area_level_3
                                    [1] => political
                                )

                        )

                    [4] => SimpleXMLElement Object
                        (
                            [long_name] => Greater London
                            [short_name] => Gt Lon
                            [type] => Array
                                (
                                    [0] => administrative_area_level_2
                                    [1] => political
                                )

                        )

                    [5] => SimpleXMLElement Object
                        (
                            [long_name] => United Kingdom
                            [short_name] => GB
                            [type] => Array
                                (
                                    [0] => country
                                    [1] => political
                                )

                        )

                )

            [geometry] => SimpleXMLElement Object
                (
                    [location] => SimpleXMLElement Object
                        (
                            [lat] => 51.5034227
                            [lng] => -0.1080750
                        )

                    [location_type] => APPROXIMATE
                    [viewport] => SimpleXMLElement Object
                        (
                            [southwest] => SimpleXMLElement Object
                                (
                                    [lat] => 51.5020958
                                    [lng] => -0.1094971
                                )

                            [northeast] => SimpleXMLElement Object
                                (
                                    [lat] => 51.5047938
                                    [lng] => -0.1067991
                                )

                        )

                    [bounds] => SimpleXMLElement Object
                        (
                            [southwest] => SimpleXMLElement Object
                                (
                                    [lat] => 51.5032242
                                    [lng] => -0.1087301
                                )

                            [northeast] => SimpleXMLElement Object
                                (
                                    [lat] => 51.5036654
                                    [lng] => -0.1075661
                                )

                        )

                )

        )

)

All is good. i now need to retriave the long and tat values from the xml? how do i only print/show these values "lat"???: for example

result->geometry->location->lat

Just almost like you pointed it out:

$url ="http://maps.googleapis.com/maps/api/geocode/xml?address=se18lu&sensor=false";
$result = simplexml_load_file($url);
echo $result->result->geometry->location->lat;

Sometimes you have to cast the simpleXML Object to a string. You can then use:

echo (string) $result->result->geometry->location->lat;

Or

echo $result->result->geometry->location->lat->__toString();

You would need to access each item individually rather than just do it in one shot. However, you need to make sure to convert the value to a string,int, float, or w/e because the default for simple xml elements is to make each item a SimpleXmlObject. So for geometry's location lat item you would do:

echo "Geometry Location's Lat: ".((string)$result->result->geometry->location->lat);

The same would be the case for the remaining lat's

Here's what I use, based in part on this answer .

function get_geocode_arr($address)
{

    $arr_location = array();
    $google_api_request_key = 'YOUR_KEY_HERE';

    $address    = str_replace (" ", "+", urlencode($address));

    $fullurl    = "https://maps.googleapis.com/maps/api/geocode/json?address=$address&sensor=true&key=$google_api_request_key";
    $string     = file_get_contents($fullurl); // get json content
    $json_res   = json_decode($string, true); //json decoder

    foreach ($json_res['results'][0]['address_components'] as $component)
    {

        switch ($component['types']) {
            case in_array('street_number', $component['types']):
                $arr_location['street_number'] = $component['long_name'];
                break;
            case in_array('route', $component['types']):
                $arr_location['street'] = $component['long_name'];
                break;
            case in_array('sublocality', $component['types']):
                $arr_location['sublocality'] = $component['long_name'];
                break;
            case in_array('locality', $component['types']):
                $arr_location['locality'] = $component['long_name'];
                break;
            case in_array('administrative_area_level_2', $component['types']):
                $arr_location['admin_2'] = $component['long_name'];
                break;
            case in_array('administrative_area_level_1', $component['types']):
                $arr_location['admin_1'] = $component['long_name'];
                break;
            case in_array('postal_code', $component['types']):
                $arr_location['postal_code'] = $component['long_name'];
                break;
            case in_array('country', $component['types']):
                $arr_location['country'] = $component['long_name'];
                break;
        }

    }

    $arr_location['location_latitude'] = $json_res['results'][0]['geometry']['location']['lat'];
    $arr_location['location_longitude'] = $json_res['results'][0]['geometry']['location']['lng'];
    $arr_location['formatted_address'] = $json_res['results'][0]['formatted_address'];


    return $arr_location;

}   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM