0

こんにちは、Google Geocode から経度と緯度の値を出力する必要があります。以下を使用して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 値を取得する必要がありますか? これらの値「緯度」のみを印刷/表示するにはどうすればよいですか???: たとえば

結果 -> 形状 -> 位置 -> 緯度

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

一度にアクセスするのではなく、各アイテムに個別にアクセスする必要があります。ただし、単純なxml要素のデフォルトでは各項目をSimpleXmlObjectにするため、値をstring、int、float、またはw/eに変換する必要があります。したがって、ジオメトリのロケーション緯度アイテムについては、次のようにします。

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

残りのラットについても同じことが言えます

于 2012-11-28T16:05:03.623 に答える