循环通过Google Places评论数组并从回调函数获取值



我正在使用google places JavaScript API来获取地点细节。我遇到两个与JavaScript有关的问题:

第一个是从位置提取用户评论。检查数组

我可以获得place.name, place.phoneplace.formatted_address值,但我无法从place.reviews数组检索评论。

我在下面的链接上找到了一些示例代码来循环通过评论数组,但我不能让它工作。我得到脚本错误,例如使用关键字forEach

Google Places JS API Show Reviews

是否有一种简单的方法从评论数组中检索值?这些是我的HTML文档顶部的脚本声明:

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places"></script>
<script> 
第二个<<p> /strong>

我正在创建一个简单的搜索应用程序,这样我就可以查看我周围的东西。我想添加的距离和时间从开始位置到场地的用户点击即。"伦敦桥= 1公里,10分钟"。

我可以使用回调函数使用谷歌矩阵距离API获得时间和距离值,但我不知道如何将值传递给我的代码的主要部分。

我已经在这里看了一些答案,但我仍然对回调函数的工作方式感到困惑:

function createMarkers(results, PlaceSearchStatus) {
    var resultcontent = ''; //stores place results to be displayed on map
    var resultdiv = document.getElementById('searchresults');
    if (PlaceSearchStatus == google.maps.places.PlacesServiceStatus.OK) {
        // if we have found something - clear map (overlays) 
        clearOverlays();
        // and create new markers by search result 
        for (var i = 0; i < results.length; i++) {
            createMarker(results[i]);

            //use the Distance Matrix API to get the distance between this venue and start location
            /distancematrix#distance_matrix_responses
            var DistanceService = new google.maps.DistanceMatrixService();
            var origin1 = new google.maps.LatLng(document.getElementById('lat').value, document.getElementById('lng').value);
            var destination1 = results[i].geometry.location;
            DistanceService.getDistanceMatrix({
                origins: [origin1],
                destinations: [destination1],
                travelMode: google.maps.TravelMode.DRIVING,
                unitSystem: google.maps.UnitSystem.METRIC,
                avoidHighways: false,
                avoidTolls: false
            }, callback);
            function callback(response, DistanceMatrixStatus) {
                if (DistanceMatrixStatus != google.maps.DistanceMatrixStatus.OK) {
                    alert('Distance matrix API Error was: ' + DistanceMatrixStatus);
                } else {
                    var origins = response.originAddresses;
                    var destinations = response.destinationAddresses;
                    for (var i = 0; i < origins.length; i++) {
                        var results = response.rows[i].elements;
                        //getting the distance value gives more accurate results than the text value but not using that
                        //alert(results[i].distance.value + ", " + results[i].duration.value);
                        //get the time and distance to this location from our start location
                        // Need to fix. this isn't working as this is inside a callback function.
                        var TimeAndDistance = results[i].distance.text + ", " + results[i].duration.text;
                    }
                }
            } // end of distance matrix code 
            //concatonate the accessible search results and add on the time and distance to the place name 
            resultcontent += '<p> <h2>' + '<div <input id="button2" type="button" class="button" value="' + results[i].place_id + '" onclick="GetIndividualPlaceDetails(this);">' + results[i].name + '</div> </h2>';

谢谢,汤姆

下面的代码用于访问评论,我正在使用另一种方法来解决我的第二个问题:

len = place.reviews.length;
for (i=0; i<len; ++i) {
resultcontent += place.reviews[i].rating + ' stars';
resultcontent += ', On ' + place.reviews[i].time + ' ' + place.reviews[i].author_name + ' said:';
if (!!place.reviews[i].text)                      resultcontent += '<br />' + place.reviews[i].text;
}
}

相关内容

  • 没有找到相关文章

最新更新