Javascript 在GooglePlaces数组中循环并从回调函数中获取值
我正在使用GooglePlacesJavaScriptAPI获取地点详细信息。 我在使用JavaScript时遇到了两个问题: 第一个是从place.reviews数组中提取用户评论 我可以获取Javascript 在GooglePlaces数组中循环并从回调函数中获取值,javascript,arrays,function,loops,Javascript,Arrays,Function,Loops,我正在使用GooglePlacesJavaScriptAPI获取地点详细信息。 我在使用JavaScript时遇到了两个问题: 第一个是从place.reviews数组中提取用户评论 我可以获取place.name、place.phone和place.formatted\u address值,但我无法从place.reviews数组中检索评论 我在下面的链接上找到了一些示例代码,可以在reviews数组中循环,但我无法让它工作。我得到脚本错误,例如关键字forEach 有没有一种简单的方法可以
place.name
、place.phone
和place.formatted\u address
值,但我无法从place.reviews
数组中检索评论
我在下面的链接上找到了一些示例代码,可以在reviews数组中循环,但我无法让它工作。我得到脚本错误,例如关键字forEach
有没有一种简单的方法可以从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>
第二个
我正在创建一个简单的搜索应用程序,以便查看周围的内容。我想添加从起始位置到用户点击地点的距离和时间,即“伦敦桥=1公里,10分钟”
我可以使用GoogleMatrix distance 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>';
函数createMarkers(结果、位置搜索状态){
var resultcontent='';//存储要在地图上显示的位置结果
var resultdiv=document.getElementById('searchresults');
if(PlaceSearchStatus==google.maps.places.PlacesServiceStatus.OK){
//如果我们发现了什么-清晰的地图(覆盖图)
clearOverlays();
//并根据搜索结果创建新标记
对于(var i=0;i resultcontent+=''+'以下代码用于访问评论,我在第二个问题中使用了另一种解决方法:
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;
}
}
len=place.reviews.length;
对于(i=0;i),回调是异步的!