Google 地图 API:搜索
我正在为我的公司开发一个运输系统,用户可以在其中输入(以及一堆其他信息)需要运送物品的目的地。我们要迁移的系统只是将此信息用作静态文本,因此人们会输入速记地址,例如“Alsip 60801”。
我最初的想法是,这种速记文本可以与 Google Maps API 完美配合,因为我可以在maps.google.com 中输入速记内容,而且通常可以正常工作。
因此,我编写了一些代码来运行地理编码他们的速记输入。不幸的是,令我惊讶的是,地理编码搜索返回的结果与maps.google.com 返回的结果有很大不同。
例如,当我在maps.google.com上搜索“Alsip 60801”时,我得到Alsip, IL 60801。 没错。但是当我使用 Google Maps API 进行搜索时,我得到 加拿大的一些 Alsip。
我应该为 Google Maps API 使用其他搜索功能吗?或者我缺少某种标志?
作为参考,这是我的代码的简短版本:
var geocode = new google.maps.Geocoder();
geocode.geocode( {'address': 'Alsip 60801' }, function(results, status) {
console.log(results[0].geometry.location);
//Outputs the LatLng of a Canada address
});
编辑 1:我忘了提及,我确实尝试在地理编码请求上设置区域。我给我们设置了它,并得到了相同的结果。
更新 - 已回答:我将 @Jitimaro 的答案标记为正确,因为将国家/地区代码添加到结果末尾似乎确实有效。我不确定这是否适用于每种输入(很难预测速记地址格式),但目前看来效果很好。
然而,我实际上正在以不同的方式处理这个问题。我实际上不是在地址末尾添加国家/地区代码,而是提供 LatLngBounds 参数。这会在搜索时给予我范围内的所有地址优惠待遇。
I'm working on a shipping system for my company where user's enter (along with a bunch of other information) the destination that something needs to get shipped to. The system that we are migrating from just used this information as static text, so people would enter shorthand addresses such as "Alsip 60801".
My original thought was that that sort of shorthand text would work perfectly with Google Maps API, because I can type shorthand things into maps.google.com, and it generally works.
So, I wrote up some code to run geocoding on their shorthand inputs. To my unfortunate surprise, the results returned from the geocode search were greatly different from those that are returned from maps.google.com.
For example, when I search for "Alsip 60801" on maps.google.com, I get Alsip, IL 60801. That's correct. But when I search using the Google Maps API, I get some Alsip up in Canada.
Is there another search function that I should be using for the Google Maps API? Or some sort of flag that I am missing?
For reference, this is the short version of my code:
var geocode = new google.maps.Geocoder();
geocode.geocode( {'address': 'Alsip 60801' }, function(results, status) {
console.log(results[0].geometry.location);
//Outputs the LatLng of a Canada address
});
Edit 1: I forgot to mention, I did try setting the region on the geocode request. I set it to us, and got the same results.
Update - Answered: I marked @Jitimaro's answer as correct, because tacking the country code onto the end of the results does seem to work. I'm not sure if that will work with every sort of input (it's hard to predict shorthand address formats), but it seems to work fine for now.
However, I actually am handling this a different way. Instead of just tacking a country code onto the end of the address, I'm actually providing a LatLngBounds parameter on the end of my geocode request. That gives all addresses within my bounds preferential treatment as it is searching.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您使用该网站进行搜索时,您应该会看到如下预览:http://maps.google .com/maps?q=Alsip+60801。当您搜索反向地理编码时,您必须解析结果以匹配您所在的国家/地区 http:// maps.googleapis.com/maps/geo?q=Alsip+60801。您可以使用网络嗅探器检查不同的 url。或者,您可以将国家/地区添加到您的网址 http://maps.googleapis。 com/maps/geo?q=Alsip+60801+USA。
When you search with the website you should get a preview like this: http://maps.google.com/maps?q=Alsip+60801. When you search a reverse geocode you must parse the result to match your country http://maps.googleapis.com/maps/geo?q=Alsip+60801. You can check the different url with a network sniffer. Or you can add the country to your url http://maps.googleapis.com/maps/geo?q=Alsip+60801+USA.