Incorrect results returned from a partial UK postcode

戏子无情 提交于 2020-01-03 16:56:42

问题


I am having an odd issue with Google Maps which their documentation doesn't seem to cover.

We have a page in our application that returns a list of engineer locations ordered by distance when an end user enters a postcode. We have recently noticed that some end users are entering partial UK postcodes (L1, BB7 or SE10).

These tend to be fine and work as intend but if the user searches for a partial postcode for the Manchester area they get odd results with distances of over 100 miles being returned.

I've researched this and it appears that Google is returning the locations for the motorway network where the road name matches the postal area in Manchester. For example: M4 is returning the motorway just north of Bristol instead of the city centre area.

The payload I am sending to Google is as follows:

$url="https://maps.google.com/maps/api/geocode/json?address={$address}&components=country:GB&key={$apikey}";

If I change the payload to this:

$url="https://maps.google.com/maps/api/geocode/json?components=country:GB|postal_code:{$address}&key={$apikey}";

I get ZERO_RESULTS from the API.

I saw this question from 2014 which is the identical issue but no resolution appears to have been found.

What am I doing wrong? Are there any workarounds that I could try to get slightly more accurate results? Am I better off restricting the inputs to full postcodes only?


回答1:


I have come up with a rather messy hack that appears to solve my issue.

// Check if the postcode is a partial Mancunian postcode
if($postcode[0]==="M" && strlen($postcode) <= 4){
    if($postcode[1]==6){
        $manchester_postcode = "Salford ".$postcode;
    }else{
        $manchester_postcode = "Manchester ".$postcode;
    }
    $address=urlencode($manchester_postcode);
}else{
    $address=urlencode($postcode);
}

$apikey=urlencode(env('GOOGLE_MAP_API'));
$url="https://maps.google.com/maps/api/geocode/json?address={$address}&components=country:GB&key={$apikey}";

If the postcode is identifiable as a partial (4 characters or less) and starts with the letter M, I prefix it with "Manchester". This solves all the mis-codings except for M6 which is still stubbornly selecting the motorway.

So I added in the check for the second character and change the prefix to "Salford". This resolves the problem.



来源:https://stackoverflow.com/questions/50842474/incorrect-results-returned-from-a-partial-uk-postcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!