这是我目前关于如何获得不同组件的代码。
function getGeocodeFull($address)
{
if (empty($address)) return false;
$_geocode = false;
if (($_geocode = $this->getCache($address)) === false) // check if address is has a cache
{
if (($results = $this->geoGetCoordsFull($address)) !== false)
{
// sloooooooooopppy.. yes these can all be combined, i know.
if (!isset($results->results[0]->geometry->location->lat)) return false;
if (!isset($results->results[0]->geometry->location->lat)) return false;
if (!isset($results->results[0]->formatted_address)) return false;
if (!isset($results->results[0]->address_components)) return false;
$_geocode['lat'] = $results->results[0]->geometry->location->lat;
$_geocode['lon'] = $results->results[0]->geometry->location->lng;
$_geocode['address'] = $results->results[0]->formatted_address;
foreach ($results->results[0]->address_components as $component)
{
if (isset($component->types))
{
foreach ($component->types as $type)
{
switch ($type)
{
case 'route':
$_geocode['street'] = $component->long_name;
break;
case 'locality':
$_geocode['city'] = $component->long_name;
break;
case 'administrative_area_level_2':
$_geocode['county'] = $component->long_name;
break;
case 'administrative_area_level_3':
$_geocode['area'] = $component->long_name;
break;
case 'postal_code':
$_geocode['zip'] = $component->short_name;
break;
}
}
}
}
$this->putCacheFull($address, $_geocode['lat'], $_geocode['lon'], $_geocode['address'], $_geocode['street'], $_geocode['city'], $_geocode['county'], $_geocode['area'], $_geocode['zip']);
}
}
return $_geocode;
}
geoGetCoordsFull() 只需在 google map api 地理编码页面上使用 file_get_contents,然后解码结果然后返回它们。
我不太喜欢它的样子。似乎它可能非常不一致。是否有任何其他更好的解决方案可以从谷歌或任何其他服务获取所有数据?
我更喜欢另一个不限制请求的服务(或者使用多个地理编码服务,如 bing/yahoo/etc 是非法的)??(抱歉抛出另一个问题)
无论如何,非常感谢您的帮助!