这很容易,只需执行一个 if 语句来检查并查看准确性。如果它太低,请尝试在没有您的前提名称的情况下使用 goecode。在下面的例子中,我尝试 addr_2 然后 addr_3 来解决用户在 addr_1 上输入名称的情况。
$addr_1 = urlencode($row["addr_line1"]);
$addr_2 = urlencode($row["addr_line2"]);
$addr_3 = urlencode($row["addr_line3"]);
$city = urlencode($row['addr_city']);
$state = urlencode($row['addr_state']);
$postal = urlencode($row['addr_postalcode']);
$country = urlencode($row['addr_country']);
$address = format_address($addr_1, $city, $state, $postal, $country);
$base_url = "http://" . MAPS_HOST . "/maps/geo?output=json&key=" . KEY;
$request_url = $base_url . "&q=" . $address;
$geocode = geocode_address($request_url);
// check geocode accuracy and try again with different data if needed
if ($geocode['accuracy'] < 6) {
// bad geocode, try addr_2
$address2 = format_address($addr_2, $city, $state, $postal, $country);
$request_url2 = $base_url . "&q=" . $address2;
$geocode = geocode_address($request_url2);
if ($geocode['accuracy'] < 6) {
// bad geocode, try addr_3
$address3 = format_address($addr_3, $city, $state, $postal, $country);
$request_url3 = $base_url . "&q=" . $address3;
$geocode = geocode_address($request_url3);
}
}
// process results in $geocode below
有关我在 format_address() 和 geocode_address() 中所做工作的更多详细信息,请参阅使用 PHP/MySQL 的地理编码地址。