0

我有以下问题。我在 php 中使用谷歌地图来获取纬度和经度的地址以及该位置的地图快照。

要获取地址,我使用以下代码:

// INITIALIZING CURL
$returnValue = NULL;
$ch = curl_init();

// SERVICE CALL
$url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=".$lat.",".$lon."&sensor=false";

// SETTING PARAMS OF CURL
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

// GETTING AND RESULTING RESULT
$result_part = curl_exec($ch);
$json = json_decode($result_part, TRUE);

我解析获得的 JSON 如下:

// PARSING RESULTS FROM JSON
if (isset($json['results'])) {
    foreach    ($json['results'] as $result_part) {
        foreach ($result_part['address_components'] as $address_component) {
            $types = $address_component['types'];
            // GETTING STREET
            if (in_array('route', $types)) {
                $addr = $address_component['long_name'];
            }
            // GETTING STREET NUMBER
            if (in_array('street_number', $types)) {
                $number = $address_component['long_name'];
            }
            // GETTING COUNTRY
            if (in_array('country', $types)) {
                $country = $address_component['long_name'];
            }
            // GETTING POSTAL CODE
            if (in_array('postal_code', $types)) {
                $postal_code = $address_component['long_name'];
            }
            // GETTING CITY
            if (in_array('locality', $types)) {
                $city = $address_component['long_name'];
            }
        }
    }
}

它工作正常,但有时无法获得地址。它看起来像一些请求超载,但我不明白为什么,因为我正在编程的网站还不能被其他人访问。

与此相关的其他问题是地图快照。这是代码:

<? echo "<a href = \"https://maps.google.com/maps?q=".$lat.",".$lon."\" target=\"_blank\">" ?>
<? echo "<img src=\"http://maps.googleapis.com/maps/api/staticmap?center=" . $lat . "," . $lon . "&zoom=16&size=200x200&markers=color:blue%7Clabel:I%7C" . $lat . "," . $lon . "&sensor=false\" alt=\"google maps\" width=\"200\" height=\"200\" /></a>" ?>

这也很好,但有时我会得到这样的图像:

在此处输入图像描述

我怀疑我超出了限制。

有任何想法吗 ?谢谢您的回答。

4

1 回答 1

2

正如@geocodezip 所建议的那样,这可能是因为没有使用密钥。

也根据反向地理编码文档:

https://developers.google.com/maps/documentation/geocoding/#ReverseGeocoding

注意:反向地理编码是一个估计值。地理编码器将尝试在某个容差范围内找到最近的可寻址位置;如果未找到匹配项,则地理编码器将返回零个结果。

于 2013-07-22T08:43:31.177 回答