为什么使用 Google Places JavaScript 客户端库 return "NaN",同时硬编码 API URL returns 准确的结果?
Why does using the Google Places JavaScript client library return "NaN", while hardcoding in a API URL returns accurate results?
我正在玩 Google 的地方 API 并正在开发一个小型项目,该项目 return 是当地便利设施(学校、酒吧、餐厅、咖啡馆)基于用户搜索的位置。使用 Google Places Library 查询 HTML 文件中 JavaScript 的结果,我发现 NaN 或 Not Any Number 正在被 return 用于评级否则就在那里,据我所知,该地区将拥有上述许多便利设施。某些区域会 return 评分,例如咖啡馆和健身房,但酒吧的评分为 NaN,其他区域则相反。为了更深入地研究这个问题,我在我的浏览器中搜索了以下 API URL,它以 XML 格式显示了我期望的特定区域健身房的所有结果(屏幕截图下面)。
https://maps.googleapis.com/maps/api/place/nearbysearch/xml?location=53.309362,-6.304930600000034&radius=1000&type=gym&key=MY_API_KEY
然而,当我 运行 通过 Place 的 Javascript 客户端库进行类似查询时,我得到了一个 NaN。 Client Library 在可查询的结果方面是否与 Google 地方 API 不相上下,还是我弄错了?
//定义我的API KEY
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=MY_API_KEY&libraries=places"></script>
// 我如何查询客户端库
function getGyms(){
//These are the laltitude and longitude values provided by the user
$('.search_latitude').val(marker.getPosition().lat());
$('.search_longitude').val(marker.getPosition().lng());
var Lat = marker.getPosition().lat();
console.log(Lat);
var Long = marker.getPosition().lng();
console.log(Long);
var gymLocation = {lat: Lat, lng: Long};
var service = new google.maps.places.PlacesService(map);
service.nearbySearch({
location: gymLocation,
radius: 2000,
type: ['gym']
}, gymCallback);
}
function gymCallback(results2, status2){
var totalRating = 0;
results2.forEach( function( place ) {
totalRating += place.rating;
});
//Calculating the average rating from the list of gyms
var averageRating = results2.length == 0 ? 0 : totalRating / results2.length;
var averageRatingRounded = averageRating.toFixed(1);
// Passing the rating to a TextBox
var averageGymRatingTB = document.getElementById('gymAvgRating');
averageGymRatingTB.value = averageRatingRounded;
}
api 调用没有问题,问题在于您如何处理代码中的结果。
有些地方没有评论,因此他们的评分是 undefined
。
您的代码尝试在 totalRating += place.rating;
行添加这些 undefined
评级,因此您得到 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;
}
我正在玩 Google 的地方 API 并正在开发一个小型项目,该项目 return 是当地便利设施(学校、酒吧、餐厅、咖啡馆)基于用户搜索的位置。使用 Google Places Library 查询 HTML 文件中 JavaScript 的结果,我发现 NaN 或 Not Any Number 正在被 return 用于评级否则就在那里,据我所知,该地区将拥有上述许多便利设施。某些区域会 return 评分,例如咖啡馆和健身房,但酒吧的评分为 NaN,其他区域则相反。为了更深入地研究这个问题,我在我的浏览器中搜索了以下 API URL,它以 XML 格式显示了我期望的特定区域健身房的所有结果(屏幕截图下面)。
https://maps.googleapis.com/maps/api/place/nearbysearch/xml?location=53.309362,-6.304930600000034&radius=1000&type=gym&key=MY_API_KEY
然而,当我 运行 通过 Place 的 Javascript 客户端库进行类似查询时,我得到了一个 NaN。 Client Library 在可查询的结果方面是否与 Google 地方 API 不相上下,还是我弄错了?
//定义我的API KEY
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=MY_API_KEY&libraries=places"></script>
// 我如何查询客户端库
function getGyms(){
//These are the laltitude and longitude values provided by the user
$('.search_latitude').val(marker.getPosition().lat());
$('.search_longitude').val(marker.getPosition().lng());
var Lat = marker.getPosition().lat();
console.log(Lat);
var Long = marker.getPosition().lng();
console.log(Long);
var gymLocation = {lat: Lat, lng: Long};
var service = new google.maps.places.PlacesService(map);
service.nearbySearch({
location: gymLocation,
radius: 2000,
type: ['gym']
}, gymCallback);
}
function gymCallback(results2, status2){
var totalRating = 0;
results2.forEach( function( place ) {
totalRating += place.rating;
});
//Calculating the average rating from the list of gyms
var averageRating = results2.length == 0 ? 0 : totalRating / results2.length;
var averageRatingRounded = averageRating.toFixed(1);
// Passing the rating to a TextBox
var averageGymRatingTB = document.getElementById('gymAvgRating');
averageGymRatingTB.value = averageRatingRounded;
}
api 调用没有问题,问题在于您如何处理代码中的结果。
有些地方没有评论,因此他们的评分是 undefined
。
您的代码尝试在 totalRating += place.rating;
行添加这些 undefined
评级,因此您得到 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;
}