2016-12-06 119 views
0

我有一個谷歌API的網址,有我想在我的網頁上輸出的JSON。谷歌地方的API解析爲javascript

的URL包括ID和關鍵

https://maps.googleapis.com/maps/api/place/details/json?placeid=122&key=123-mg 

現在我想在div id myoutput打印出以下數據

"reviews" : [ 
     { 
      "aspects" : [ 
       { 
        "rating" : 3, 
        "type" : "overall" 
       } 
      ], 
      "author_name" : "Justine OBRIEN", 
      "author_url" : "https://www.google.com/maps/contrib/104177669626132953795/reviews", 
      "language" : "en", 
      "profile_photo_url" : "//lh6.googleusercontent.com/-s6AzNe5Qcco/AAAAAAAAAAI/AAAAAAAAFTE/NvVzCuI-jMI/photo.jpg", 
      "rating" : 5, 
      "relative_time_description" : "2 months ago", 
      "text" : "Fabulous location. Wonderful warm welcoming reception. Excellent unique living Google wall entry. Sensational helpful kind people. Easy fast efficient help online plus with appointment on site. Super company always progressive plus innovative products and services for all businesses. Go Google Australia. Shine on! ", 
      "time" : 1474966415 
     }, 

我迄今試圖

<div id="myoutput"></div> 

      <script> 
       (function() { 
        var myobject = "https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJN1t_tDeuEmsRUsoyG83frY4&key=AIzaSyCUc3pmryE8WMd7niIrHIN7cq_6ZUyI-mg"; 
        $.getJSON(myobject, { 

         format: "json" 
        }) 
          .done(function(data) { 
           $.each(data.items, function(i, item) { 
            $("<p>").attr("author_name", item.media.m).appendTo("#myoutpu"); 
            if (i === 3) { 
             return false; 
            } 
           }); 
          }); 
       })(); 
      </script> 

<script> 
       (function() { 
        var myobject = "https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJN1t_tDeuEmsRUsoyG83frY4&key=AIzaSyCUc3pmryE8WMd7niIrHIN7cq_6ZUyI-mg"; 
        $.getJSON(myobject, { 
         format: "json" 
        }) 

        print myobject.dumps([s['formatted_address'] for s in result['results']], indent=2) 
        myobject["address_components"].long_name 

       })(); 
      </script> 

我怎麼看不到author_name打印出來?

+0

從代碼示例中刪除您的密鑰,並對其進行更改。 – yomisimie

+0

它不是我的鑰匙,但我會如此 –

回答

1

你的方法似乎是在這種情況下CORS被封鎖,所以你可以使用的地方圖書館,像這樣:

<div id="map"></div> 
<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_KEY&libraries=places&callback=initMap"></script> 
<script> 
function initMap() { 
    var service = new google.maps.places.PlacesService(map); 
    service.getDetails({ 
    placeId: 'ChIJN1t_tDeuEmsRUsoyG83frY4' 
    }, function(place, status) { 
    if (status === google.maps.places.PlacesServiceStatus.OK) { 
     for(var i=0; i <= place.reviews.length; i++) { 
     console.log(place.reviews[i]); 
     } 
    } 
    }); 
} 
</script> 

map格在那裏只是爲了運作服務。循環是您逐一顯示所有評論對象的地方。在那裏你可以做你所需要的結果。 此外,您可以從鏈接中刪除&callback=initMap並在需要時運行該功能。

+0

嗯,明白,生病嘗試,並得到這個工作,看看有什麼附加。感謝接受,如果我得到它的工作 –

+0

它基本上是一樣的東西,你只是使用一個正常的谷歌地圖API的函數。如果你爲此而苦惱,我很樂意幫助你。 – yomisimie

+0

非常感謝你,生病回覆很快(20分鐘) –