【问题标题】:php $json google maps parsingphp $json 谷歌地图解析
【发布时间】:2012-04-13 17:37:16
【问题描述】:

我有以下代码:

<?php
$q = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=35.0078,-97.0929&destinations=45.2078,-97.0929&mode=driving&sensor=false"; 

$json = file_get_contents($q); 

$details = json_decode($json, TRUE); 

echo "<pre>"; print_r($details); echo "</pre>"; 
?>

结果是:

Array
(
    [destination_addresses] => Array
        (
            [0] => 150th St, Summit, SD 57266, USA
        )

    [origin_addresses] => Array
        (
            [0] => 38591 Patterson Rd, Wanette, OK 74878, USA
        )

    [rows] => Array
        (
            [0] => Array
                (
                    [elements] => Array
                        (
                            [0] => Array
                                (
                                    [distance] => Array
                                        (
                                            [text] => 1,299 km
                                            [value] => 1299343
                                        )

                                    [duration] => Array
                                        (
                                            [text] => 13 hours 26 mins
                                            [value] => 48361
                                        )

                                    [status] => OK
                                )

                        )

                )

        )

    [status] => OK
)

我只需要从 $json 中提取距离(1,299 公里)。 我不知道该怎么做。 非常感谢任何帮助。

【问题讨论】:

标签: json parsing maps


【解决方案1】:
<?php $q = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=35.0078,-97.0929&destinations=45.2078,-97.0929&mode=driving&sensor=false"; 

$json = file_get_contents($q);
$details = json_decode($json);
$distance=$details->rows[0]->elements[0]->distance->text;
echo $distance; ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-12
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多