2012-07-25 34 views
5

這可能很基本,但我試圖測試Google Places API。我正在瀏覽文檔並使用他們提供的一些示例。我試圖使用JQuery getJSON函數,因爲我已經能夠成功地使用它來異步訪問外部JSON文件,所以我認爲這可能是獲取Google Places查詢的JSON結果的好方法。這是我想要使用的代碼:試圖通過JQuery的getJSON函數使用Google Places API

<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喬治街,T他Rocks NSW,澳大利亞 悉尼新南威爾士州悉尼喬治街483號

這意味着它的工作原理。 getJSON函數不能正確解析JSON腳本?

+0

看到這個答案 http://stackoverflow.com/questions/5564830/parsing- google-geo-api-reverse-geocoding -with-jquery/39276042#answer-39276042 – 2016-09-01 16:01:53

回答

6

我會推薦使用Google Maps JavaScript API v3Places Library

你可以找到演示和如何使用它的文檔here

+0

這可能會起作用,但我很好奇爲什麼這段代碼不能用遠程JSON腳本工作。據我所知,Google並沒有真正展示如何使用AJAX來使用Google Places服務的有價值的例子。人們通常如何做到這一點?感謝您的反應。 – rocklandcitizen 2012-07-28 20:22:34

2

好吧,我或多或少地想通了。我想出了你需要做的事情,以便getJSON函數返回JSON解析的數據。你必須添加「callback =?」到查詢字符串。

'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的響應時間,格式爲有效這是奇怪的。此外,如果從本地文件讀取,則相同的響應將起作用。

+1

需要'回調'來將JSON輸出轉換爲JSONP輸出。只有JSONP可以用於JQM。 – 2014-04-18 11:50:29

1

請嘗試以下代碼

 <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的回調,因爲它是跨域調用

+0

你的鏈接不正確!你曾試過? – 2016-09-01 14:55:30