尝试使用谷歌Places API的使用jQuery的功能的getJSON(Trying to use

2019-06-26 12:36发布

这可能是非常基本的,但我想,以测试谷歌Places API的。 我经历的文档,并使用一些他们提供的例子。 我试图使用的JQuery的getJSON功能,因为我已经能够成功地使用它来访问外部JSON文件异步有了它,所以我认为这可能是一个很好的方法来获得谷歌的地方查询的JSON结果。 这是我想要使用的代码:

<body>
<div id="message"></div>
<script type="text/javascript">

    var requestURL = 'https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key='my_google_places_key';
    $(document).ready(function () {
        $.getJSON(requestURL, function (data) {

            for (i = 0; i < data.results.length; i++) {
                myAddress[i] = data.results[i].formatted_address;
                document.getElementById("message").innerHTML += myAddress[i] + "<br>";
                console.log(myAddress[i]);
            }

        });
    });  


</script>

</body>

根据该文件生成的JSON响应查询应如下:

{
   "html_attributions" : [
      "Listings by \u003ca href=\"http://www.yellowpages.com.au/\"\u003eYellow Pages\u003c/a\u003e"
   ],
   "results" : [
      {
         "formatted_address" : "529 Kent Street, Sydney NSW, Australia",
         "geometry" : {
            "location" : {
               "lat" : -33.8750460,
               "lng" : 151.2052720
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
         "id" : "827f1ac561d72ec25897df088199315f7cbbc8ed",
         "name" : "Tetsuya's",
         "rating" : 4.30,
         "reference" : "CnRmAAAAmmm3dlSVT3E7rIvwQ0lHBA4sayvxWEc4nZaXSSjRtfKRGoYnfr3d5AvQGk4e0u3oOErXsIJwtd3Wck1Onyw6pCzr8swW4E7dZ6wP4dV6AsXPvodwdVyqHgyGE_K8DqSp5McW_nFcci_-1jXb5Phv-RIQTzv5BjIGS0ufgTslfC6dqBoU7tw8NKUDHg28bPJlL0vGVWVgbTg",
         "types" : [ "restaurant", "food", "establishment" ]
      },
      {
         "formatted_address" : "Upper Level, Overseas Passenger Terminal/5 Hickson Road, The Rocks NSW, Australia",
         "geometry" : {
            "location" : {
               "lat" : -33.8583790,
               "lng" : 151.2100270
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/cafe-71.png",
         "id" : "f181b872b9bc680c8966df3e5770ae9839115440",
         "name" : "Quay",
         "rating" : 4.10,
         "reference" : "CnRiAAAADmPDOkn3znv_fX78Ma6X5_t7caEGNdSWnpwMIdDNZkLpVKPnQJXP1ghlySO-ixqs28UtDmJaOlCHn18pxpj7UQjRzR4Kmye6Gijoqoox9bpkaCAJatbJGZEIIUwRbTNIE_L2jGo5BDqiosqU2F5QdBIQbXKrvfQuo6rmu8285j7bDBoUrGrN4r6XQ-PVm260PFt5kwc3EfY",
         "types" : [ "cafe", "bar", "restaurant", "food", "establishment" ]
      },
      {
         "formatted_address" : "107 George Street, The Rocks NSW, Australia",
         "geometry" : {
            "location" : {
               "lat" : -33.8597750,
               "lng" : 151.2085920
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
         "id" : "7beacea28938ae42bcac04faf79a607bf84409e6",
         "name" : "Rockpool",
         "rating" : 4.0,
         "reference" : "CnRlAAAAVK4Ek78r9yHV56I-zbaTxo9YiroCbTlel-ZRj2i6yGAkLwNMm_flMhCl3j8ZHN-jJyG1TvKqBBnKQS2z4Tceu-1kZupZ1HSo5JWRBKd7qt2vKgT8VauiEBQL-zJiKVzSy5rFfilKDLSiLusmdi88ThIQqqj6hKHn5awdj6C4f59ifRoUg67KlbpuGuuW7S1tAH_EyBl6KE4",
         "types" : [ "restaurant", "food", "establishment" ]
      },
      {
         "formatted_address" : "483 George Street, Sydney NSW, Australia",
         "events" : [
            {
              "event_id" : "7lH_gK1GphU",
              "summary" : "Google Maps Developer Meetup: Rockin' out with the Places API",
              "url" : "https://developers.google.com/places"
            }
          ],
         "geometry" : {
            "location" : {
               "lat" : -33.8731950,
               "lng" : 151.2063380
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/civic_building-71.png",
         "id" : "017049cb4e82412aaf0efbde890e82b7f2987c16",
         "name" : "Chinatown Sydney",
         "rating" : 4.0,
         "reference" : "CnRuAAAAsLNeRQtKD7TEUXWG6gYD7ByOVKjQE61GSyeGZrX-pOPVps2BaLBlH0zBHlrVU9DKhsuXra075loWmZUCbczKDPdCaP9FVJXB2NsZ1q7188pqRFik58S9Z1lcWjyVoVqvdUUt9bDMLqxVT4ENmolbgBIQ9Wy0sgDy0BgWyg5kfPMHCxoUOvmhfKC-lTefXGgnsRqEQwn8M0I",
         "types" : [
            "city_hall",
            "park",
            "restaurant",
            "doctor",
            "train_station",
            "local_government_office",
            "food",
            "health",
            "establishment"
         ]
      }
   ],
   "status" : "OK"
}

如果我复制此JSON脚本,并将其保存到一个文件,我可以访问它,它显示在浏览器上以下结果:

529肯特街,悉尼新南威尔士州,澳大利亚上层,海外客运/ 5希克森路,岩石区澳大利亚新南威尔士州107乔治街,岩石区澳大利亚新南威尔士州483乔治街,悉尼新南威尔士州,澳大利亚

这意味着它的工作原理。 是的getJSON功能无法正常解析JSON脚本?

Answer 1:

我会建议使用的商家信息库中的谷歌地图的JavaScript API V3 。

你可以找到如何使用它演示的和文档在这里 。



Answer 2:

好吧,我或多或少想通了。 我想通了,你需要做的,这样的的getJSON函数返回JSON解析的数据是什么。 你必须添加“回调=?” 到查询字符串。

'https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key="myKey"&callback=?';

然而,现在的问题是,我现在在我的控制台说,得到一个错误:

SyntaxError: invalid label
[Break On This Error]   

"html_attributions" : [

 json?l...0080533 (line 2, col 3)

这是奇怪,因为我查JSONLint的响应和格式是有效的。 此外,如果它从本地文件读取相同的响应工作。



Answer 3:

请尝试下面的代码

      <body>
     <div id="message"></div>
     <script type="text/javascript">
        $(document).ready(function () {
        $.ajax({
        type: 'GET',
        url: 'https://maps.googleapis.com/maps/api/place/searc/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key='my_google_places_key';',
        async: false,
        jsonpCallback: 'jsonCallback',
        contentType: "application/json",
        dataType: 'jsonp',
        success: function (data) {
             for (i = 0; i < data.results.length; i++) {
            myAddress[i] = data.results[i].formatted_address;
            document.getElementById("message").innerHTML += myAddress[i] + "<br>";
            console.log(myAddress[i]);
        };
        },
        error: function (e) {
            console.log(e.message);
        }
    });
});  
 </script>
 </body>

你需要使它为JSON的回调,因为它是跨域调用



文章来源: Trying to use Google Places API with JQuery's getJSON function