使用jQuery查询Google Places API [英] Querying Google Places API using jQuery

查看:73
本文介绍了使用jQuery查询Google Places API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google Places API现在普遍可用.我正在尝试使用jQuery中的.ajax()调用来调用Google Places.我一直找回来的错误是 Uncaught SyntaxError:Unexpected token:

Google Places API is now generally available. I am trying to use the .ajax() call in jQuery to make a call to Google Places. The error I keep getting back is Uncaught SyntaxError: Unexpected token :

我正在使用jQuery 1.5.2.我也尝试了1.5.1,但是结果相同.如果可以的话,我宁愿不要升级到1.6.1.

I am using jQuery 1.5.2. I tried on 1.5.1 too, but that had the same results. I'd rather not move to 1.6.1 if I can help it.

从那时起,我就已经向其他API进行了类似的ajax调用,但是在使用Google地方信息时遇到了麻烦.以下是您可以使用的非常基本的代码示例.您需要在Google提供的API控制台(https://code.google.com/apis/console)中获取自己的密钥

I've made ajax calls like this to other APIs just since, but am having trouble with Google Places. Below is a very basic sample of code you can play with. You'll need to go get your own key at the API Console Google offers (https://code.google.com/apis/console)

jQuery.noConflict();
 jQuery(document).ready(function(){

  var url = 'https://maps.googleapis.com/maps/api/place/search/json';

  jQuery.ajax({
   url: url,
   dataType: 'jsonp',
   type: 'GET',
   data:  {
     location: '33.787794,-117.853111',
     radius: 1000,
     name: 'coffee',
     key: 'your_key', // add your key here
     sensor: 'false'
     },

   // on success
   success: function(data, textStatus, jqXHR){

      console.log(data);


   },

   // on failure
   error: function (jqXHR, textStatus, errorThrown){
    console.log(jqXHR);
    console.log(textStatus);
    console.log(errorThrown);
   }
   });
 });

推荐答案

从文档中可以看到,Google Places Web Service API 不支持JSONP-仅支持JSON.您看到的错误是因为响应仅是JSON,但是正在被解析为JSONP,从而导致错误.

From what I can see from the documentation, the Google Places Web Service API does not support JSONP - only JSON. The error you're seeing is because the response is simply JSON but is being parsed as if it was JSONP and that causes an error.

检出Google Maps JavaScript API-它包括您可能可以使用的Places库-请参见

Check out the Google Maps JavaScript API - it includes a Places library that you might be able to use - see google.maps.places.PlacesServices#search().

AFAIK,似乎已朝着取消JSONP支持的方向发展-例如,在v2中用于支持JSONP(未记录)的Geocoding API,而在v3中已不再支持.有人建议这样做可能是为了鼓励开发人员改用JavaScript API.

AFAIK, there seems to be shift towards removing JSONP support - for example, the Geocoding API used to support JSONP (undocumented) in v2 but no longer in v3. Someone suggested it might be in order to encourage developers to use the JavaScript API instead.

这篇关于使用jQuery查询Google Places API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆