Google 地图/地点可以“自动完成”吗? API可以通过AJAX使用吗?
我正在尝试使用 Google Places 自动完成 API 在网络应用程序上使用机构数据预填写表单,以简化数据输入。 API 非常简单,但它似乎不想接受 XHR。
$.getJSON("https://maps.googleapis.com/maps/api/place/autocomplete/json",{
input: input.term,
sensor: false,
types: 'establishment',
location: '40.01496,-105.27029',
radius: 10000,
key: Config.googleplaceskey
},function(places_response){
//Some other code.
});
我在控制台中收到此消息:
XMLHttpRequest 无法加载https://maps.googleapis.com/maps/api/place/autocomplete/json?input=At&sensor=false&types=builtment&location=40.01496%2C-105.27029&radius=10000&key=AIzaSyDKzUgcLklQE_U5494vHq_SzrFakNHugaQ。 Access-Control-Allow-Origin 不允许 Origin http://localhost:8086。
这是否不是 API 的用途?有人知道解决方法,或者我可以发送某种额外的参数来使其工作吗?
更新:
以下是此调用的 API 文档的链接。父文档实际上甚至有 JavaScript JSON 解析示例。真的很令人困惑为什么这会在服务器端关闭。
http://code.google.com/apis/maps/documentation/places /autocomplete.html
I'm trying to use the Google Places autocomplete API to pre-fill a form on a web application with Establishment data to ease data-entry. The API is pretty straightforward, but it doesn't seem to want to accept the XHR.
$.getJSON("https://maps.googleapis.com/maps/api/place/autocomplete/json",{
input: input.term,
sensor: false,
types: 'establishment',
location: '40.01496,-105.27029',
radius: 10000,
key: Config.googleplaceskey
},function(places_response){
//Some other code.
});
I get this in the console:
XMLHttpRequest cannot load https://maps.googleapis.com/maps/api/place/autocomplete/json?input=At&sensor=false&types=establishment&location=40.01496%2C-105.27029&radius=10000&key=AIzaSyDKzUgcLklQE_U5494vHq_SzrFakNHugaQ. Origin http://localhost:8086 is not allowed by Access-Control-Allow-Origin.
Is this somehow not what the API is meant for? Anyone know a workaround, or some kind of extra parameter(s) I could send to make it work?
Update:
Here's the link to the API documentation for this call. The parent docs actually even have JavaScript JSON-parsing examples. Really confusing why this would be shut down on the server side.
http://code.google.com/apis/maps/documentation/places/autocomplete.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这是一个代码片段,供未来遇到这种情况的读者参考。
使用“Places API”而不是“Maps API”,此代码片段使用 Google 返回的数据填充我的表单元素(包括用于自动完成的输入)。
在“place_changed”的自动完成上放置一个监听器(他们从列表中选择了一些内容),然后用返回的数据填充输入。
所有这些都列在Google 地点库页面上。
Here is a code snippet for future readers who come across this scenario.
Using the "Places API" rather than the "Maps API", this code snippet fills in my form elements (including the input that is used to autocomplete) with returned data from Google.
Put a listener on the autocomplete for "place_changed" (they chose something from the list) and then fill in the inputs with the data returned.
All of this is listed on the Place Library Google page.
要进行这样的跨域 AJAX 调用,您应该使用带有回调的 JSONP。不过,Google 不提供用于自动完成的 JSONP 接口。
您也许可以改用此自动完成类,尽管似乎无法设置下拉菜单的样式。
编辑可能有办法做到这一点。查看 geo-autocomplete jQuery 插件。它有两个演示。然而,我还没有对此进行适当的审视。
To make cross-domain AJAX calls like this, you should use JSONP with a callback. However, Google doesn't provide a JSONP interface for Autocomplete.
You might be able to use this Autocomplete class instead, though it doesn't seem possible to style the dropdown.
EDIT There may be a way to do this. Check out the geo-autocomplete jQuery Plugin. It has two demos. I haven't given this a proper look over yet however.
有两种方法可以访问 Google 地图自动完成 API。
第一个是通过
google.maps.places.Autocomplete
类。这提供了 Javascript 中所有必要的实现。但是,您可以完全控制样式。使用pac-container
和pac-item
CSS 类。第二种是通过自动完成网络服务。由于同源策略(没有 JSONP API),无法通过浏览器访问此内容。这是访问自动完成结果的最灵活的方式。
There are two ways to access the Google Maps Autocomplete API.
The first is via the
google.maps.places.Autocomplete
class. This provides all the necessary implementation in Javascript. However, you have complete control over styling. Use thepac-container
andpac-item
CSS classes.The second is via the Autocomplete Web Service. This is not accessible via the browser because of same origin policy (there is no JSONP API). This is the most flexible way to access Autocomplete results.
Google 可能只允许通过其自己的 Javascript API 调用此 API,您可以从maps.google.com 提供的 Javascript 文件加载该 API。缺少 Access-Control-Allow-Origin 标头表明您不应该通过 Javascript 使用 API。
或者,您可以简单地编写一个服务器端代理函数,该函数调用 Google API 并将结果转发到您自己的 $.getJSON 调用,但这可能违反服务条款。
http://www.daniweb.com/web-development/php/code/216729
(免责声明:我还没有阅读此特定函数调用的 API 规范)
The Google probably allows this API only called through its own Javascript API which you load from maps.google.com served Javascript file. The lack of Access-Control-Allow-Origin header tells that you should not use the API otherwise through Javascript.
Alternatively you can simply write a server-side proxy function which calls Google API and passes the result forward to your own $.getJSON call, but this would be probably against the Terms of Service.
http://www.daniweb.com/web-development/php/code/216729
(disclaimer: I have not read the API spec for this particular function call)
您可以拦截返回的 JSONP 结果通过 google.maps.places.Autocomplete 功能并根据需要使用它们。
基本上,您在 head 元素上重新定义了appendChild 方法,然后监视Google 自动完成代码插入到JSONP DOM 中的javascript 元素。添加 javascript 元素时,您可以覆盖 Google 定义的 JSONP 回调,以便访问原始自动完成数据。
这有点像黑客,如下(我正在使用 jQuery,但这个黑客没有必要工作):
You can intercept the JSONP results that are returned by the google.maps.places.Autocomplete functionality and use them as you see fit.
Basically you redefine the appendChild method on the head element, and then monitor the javascript elements that the Google autocomplete code inserts into the DOM for JSONP. As javascript elements are added, you override the JSONP callbacks that Google defines in order to get access to the raw autocomplete data.
It's a bit of a hack, here goes (I'm using jQuery but it's not necessary for this hack to work):
这是一个跨域请求。默认情况下,浏览器会阻止来自跨域站点的响应。您需要使用 jsonp 作为数据类型。只要用 google 搜索一下,你就可以看到如何使用 jquery API 来完成它。堆栈溢出也有围绕这些的问题。
在同源策略下,server1.example.com 提供的网页无法正常连接或与除 server1.example.com 以外的服务器通信。 HTML 元素是一个例外。利用元素的开放策略,某些页面使用它们来检索对来自其他来源的动态生成的 JSON 格式数据进行操作的 Javascript 代码。这种使用模式称为 JSONP。
请注意,Google Places 不兼容 JSONP,因此我使用了 Google Places JavaScript API。
This is a cross domain request. Browsers by default block responses from cross domain sites. You need to use jsonp as the datatyoe. Just google the same and you can see how it can be done using the jquery API. Stack overflow has questions around these too.
Under the same origin policy, a web page served from server1.example.com cannot normally connect to or communicate with a server other than server1.example.com. An exception is the HTML element. Taking advantage of the open policy for elements, some pages use them to retrieve Javascript code that operates on dynamically-generated JSON-formatted data from other origins. This usage pattern is known as JSONP.
Note that Google Places is not JSONP compliant so I used the Google Places JavaScript API instead.
使用 Google Maps Javascript API 中的地点库:
Javascript 地点自动完成
Places API 与 Maps API 不同。
Use the Places Library from the Google Maps Javascript API:
Javascript Places Autocomplete
The Places API is different from the Maps API.
您可以使用 PHP 绕过此问题。
在 PHP 脚本中,使用 file_get_contents 来查询 URL,如下所示:
$suggestions = json_decode(file_get_contents('https://maps.googleapis.com/maps/api/place/autocomplete/json?input=blabla&sensor=false&types) = Installation&radius=10000')
然后您可以对 PHP 脚本使用 AJAX 请求,该请求将有效绕过跨域返回结果约束。
You can bypass this issue by using PHP.
In your PHP script use file_get_contents to query the URL like this:
$suggestions = json_decode(file_get_contents('https://maps.googleapis.com/maps/api/place/autocomplete/json?input=blabla&sensor=false&types=establishment&radius=10000')
You can then just use an AJAX request to your PHP script which will return the results effectively bypassing the cross domain constraint.