【发布时间】:2013-07-20 11:44:09
【问题描述】:
我有以下问题。我在 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>" ?>
这也很好,但有时我会得到这样的图像:
我怀疑我超出了限制。
有什么想法吗?谢谢你的回答。
【问题讨论】:
-
`我怀疑我超出了限制。有什么想法吗?`这是在共享网络主机(如 RackSpace)上吗?
-
不,它不在这样的共享虚拟主机上。它在自己的网络服务器上 - 但这个网络也用于公司的网页。谷歌是否有可能通过一个 IP 控制该公司所有人员的访问?我的意思是连接在该 IP 上的整个用户组
-
因为请求太多而阻塞了地图服务?
-
那是certainly possible。您似乎没有使用an API key
标签: php google-maps web map geolocation