0

您好,我需要从 Google Geocode 打印 long 和 lat 值。我已经使用以下方法检索了 xml:

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

它返回以下内容:

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
                                )

                        )

                )

        )

)

一切都很好。我现在需要从 xml 中检索 long 和 tat 值吗?我如何打印/显示这些值“lat”???:例如

结果->几何->位置->纬度

4

3 回答 3

1

就像你指出的那样:

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

有时您必须将 simpleXML 对象转换为字符串。然后你可以使用:

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

或者

echo $result->result->geometry->location->lat->__toString();
于 2012-11-28T16:05:40.000 回答
0

这是我使用的,部分基于这个答案

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;

}   
于 2014-09-04T21:19:49.217 回答
0

您需要单独访问每个项目,而不是一次性完成。但是,您需要确保将值转换为字符串、int、float 或 w/e,因为简单 xml 元素的默认设置是使每个项目成为 SimpleXmlObject。因此,对于几何的位置 lat 项目,您将执行以下操作:

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

其余的纬度也是如此

于 2012-11-28T16:05:03.623 回答