为什么使用Google Places JavaScript客户端库返回;南",;,而API URL中的硬编码会返回准确的结果吗?

为什么使用Google Places JavaScript客户端库返回;南",;,而API URL中的硬编码会返回准确的结果吗?,javascript,google-maps-api-3,google-places-api,Javascript,Google Maps Api 3,Google Places Api,我正在玩谷歌的PlacesAPI,正在做一个小项目,根据用户搜索的位置返回当地设施(学校、酒吧、餐厅、咖啡馆)的平均评分。使用GooglePlaces库在HTML文件中查询JavaScript的结果,我发现NaN,或者不是任何数字,被返回的评级应该在那里,因为我知道,事实上,该地区会有上面提到的许多便利设施。有些地区会返回咖啡馆和健身房的评级,但酒吧会返回NaN,反之亦然。为了更深入地研究这个问题,我在我的浏览器中搜索了以下API URL,它以XML格式显示了所有结果,这是我对某个特定区域的健身

我正在玩谷歌的PlacesAPI,正在做一个小项目,根据用户搜索的位置返回当地设施(学校、酒吧、餐厅、咖啡馆)的平均评分。使用GooglePlaces库在HTML文件中查询JavaScript的结果,我发现NaN,或者不是任何数字,被返回的评级应该在那里,因为我知道,事实上,该地区会有上面提到的许多便利设施。有些地区会返回咖啡馆和健身房的评级,但酒吧会返回NaN,反之亦然。为了更深入地研究这个问题,我在我的浏览器中搜索了以下API URL,它以XML格式显示了所有结果,这是我对某个特定区域的健身房所期望的(下面的屏幕截图)

然而,当我通过Place的Javascript客户端库运行类似的查询时,我得到了一个NaN。客户端库与谷歌Posiple API在查询结果上是否不符,或者是我犯了错误吗? //定义我的API密钥

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=MY_API_KEY&amp;libraries=places"></script>

api调用没有问题,问题在于如何在代码中处理结果

有些地方没有评论,因此它们的评级是
未定义的

您的代码尝试在
totalRating+=place.rating处添加这些
未定义的
评级行,从而得到
NaN
(不是数字)

你可以忽略这些(但在计算平均值时也要考虑到这一点)

差不多

function gymCallback(results2, status2) {
    var totalRating = 0,
        ratedCount = 0; // used to count how many places have a rating

    results2.forEach(function( place ) {
        if (place.rating !== undefined) {
            ratedCount++; // increase counter
            totalRating += place.rating;
        }
    });

    //Calculating the average rating from the list of gyms
    var averageRating = results2.length == 0 ? 0 : totalRating / ratedCount; // use the counter to get the average since not all results were used for the totalRating
    var averageRatingRounded = averageRating.toFixed(1);

    // Passing the rating to a TextBox
    var averageGymRatingTB = document.getElementById('gymAvgRating');
    averageGymRatingTB.value = averageRatingRounded;
}

api调用没有问题,问题在于如何在代码中处理结果

有些地方没有评论,因此它们的评级是
未定义的

您的代码尝试在
totalRating+=place.rating处添加这些
未定义的
评级行,从而得到
NaN
(不是数字)

你可以忽略这些(但在计算平均值时也要考虑到这一点)

差不多

function gymCallback(results2, status2) {
    var totalRating = 0,
        ratedCount = 0; // used to count how many places have a rating

    results2.forEach(function( place ) {
        if (place.rating !== undefined) {
            ratedCount++; // increase counter
            totalRating += place.rating;
        }
    });

    //Calculating the average rating from the list of gyms
    var averageRating = results2.length == 0 ? 0 : totalRating / ratedCount; // use the counter to get the average since not all results were used for the totalRating
    var averageRatingRounded = averageRating.toFixed(1);

    // Passing the rating to a TextBox
    var averageGymRatingTB = document.getElementById('gymAvgRating');
    averageGymRatingTB.value = averageRatingRounded;
}

很好的回答,完美地解决了问题。很好的回答,完美地解决了问题。
function gymCallback(results2, status2) {
    var totalRating = 0,
        ratedCount = 0; // used to count how many places have a rating

    results2.forEach(function( place ) {
        if (place.rating !== undefined) {
            ratedCount++; // increase counter
            totalRating += place.rating;
        }
    });

    //Calculating the average rating from the list of gyms
    var averageRating = results2.length == 0 ? 0 : totalRating / ratedCount; // use the counter to get the average since not all results were used for the totalRating
    var averageRatingRounded = averageRating.toFixed(1);

    // Passing the rating to a TextBox
    var averageGymRatingTB = document.getElementById('gymAvgRating');
    averageGymRatingTB.value = averageRatingRounded;
}