部分英国邮政编码返回的结果不正确 [英] Incorrect results returned from a partial UK postcode
问题描述
我在使用Google Maps时遇到了一个奇怪的问题,他们的文档似乎没有涵盖该问题.
I am having an odd issue with Google Maps which their documentation doesn't seem to cover.
我们的应用程序中有一个页面,当最终用户输入邮政编码时,该页面会返回按距离排序的工程师位置列表.我们最近注意到,某些最终用户正在输入部分英国邮政编码(L1
,BB7
或SE10
).
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
).
这些功能通常都可以正常工作,并且可以按预期工作,但是如果用户搜索曼彻斯特地区的部分邮政编码,则会得到奇怪的结果,并且返回的距离超过100英里.
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.
我已经对此进行了研究,看来Google正在返回高速公路名称与曼彻斯特邮政区域相匹配的高速公路网络的位置.例如:M4正在返回布里斯托尔以北而不是市中心的高速公路.
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.
我要发送给Google的有效负载如下:
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}";
我从API中获得了ZERO_RESULTS
.
我从2014年开始看到了这个问题,这是相同的问题,但似乎没有找到解决方法.
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?
推荐答案
我想出了一个相当凌乱的黑客工具,似乎可以解决我的问题.
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}";
如果邮政编码可识别为部分(少于4个字符),并且以字母M
开头,则我将其前缀为曼彻斯特".这解决了所有错误编码,除了M6
仍然顽固地选择高速公路.
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.
因此,我在检查中添加了第二个字符,并将前缀更改为"Salford".这样可以解决问题.
So I added in the check for the second character and change the prefix to "Salford". This resolves the problem.
这篇关于部分英国邮政编码返回的结果不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!