2016-12-06 6 views
0

私のページに出力するjsonのgoogle API URLがあります。javascriptへのgoogle places APIの解析

IDと

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

は今、私は私がこれまで

<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> 

を試してみましたが、どのような次のようなデータ

"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でプリントアウトしたいキーを含むURL

<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 divがちょうど機能するにはサービスのためにそこにあります。ループでは、すべてのレビューオブジェクトを1つずつ表示します。そこでは、結果に必要なものを行うことができます。 また、リンクから&callback=initMapを削除し、必要に応じてこの機能を実行することもできます。

+0

うーん、理解して、悪い試して、この作業を取得し、何を追加参照してください。おかげで病気を受け入れるのですか? –

+0

基本的には同じことですが、Google Maps APIには普通の機能を使用するだけです。あなたがそれに苦しんでいるなら、私はあなたを助けてうれしいです。 – yomisimie

+0

大変ありがとうございます。すぐに病気になります(20分) –