我试图简单地从用户那里得到一组位置。这些位置是从代码中较早的数据库中调用的。 “places”数组有3个条目,每个条目包含一些信息和“距离”和“持续时间”字段,它们都设置为0作为占位符。我试图迭代DistanceMatrix“distance.text”和“duration.text”结果并将它们添加到相应对象的“距离”和“持续时间”字段,但我要么不能在回调中使用places [i]函数或者,如果我尝试在回调之外添加值,它将丢失返回的值。如果有人能帮助我,我会非常高兴。
var service = new google.maps.DistanceMatrixService();//request distance matrix
var outputdiv = document.getElementById('info');
for (var i=1;i<places.length;i++){
var goto = new google.maps.LatLng(places[i].lat, places[i].lng);
service.getDistanceMatrix(
{
origins: [foundlatlng],
destinations: [goto],
travelMode: google.maps.TravelMode.WALKING,
unitSystem: google.maps.UnitSystem.IMPERIAL,
avoidHighways: false,
avoidTolls: false,
}, callback);//end service.getdistancematrix()
function callback(response, status) {
var distancefield = distancefield;
if (status == google.maps.DistanceMatrixStatus.OK) {
var origins = response.originAddresses;
var destinations = response.destinationAddresses;
var results = response.rows[0].elements;
for (var r = 0; r < results.length; r++) {
var element = results[r];
var distancetext = element.distance.text;
var durationtext = element.duration.text;
var to = destinations[r];
}//end for r
}//end if status=ok
}//end callback
};//end for i
console.log(places);
非常感谢你提供任何帮助!!
答案 0 :(得分:0)
Distance Matrix调用是异步的,您需要更新数组并使用回调函数中返回的值。
(console.log(places)将始终在日期返回之前运行)
我没有在你的问题中看到任何代码将返回的数据添加到你的“places”数组中,这应该在回调例程中,你现在正在做的就是在本地范围内创建变量而不是回调函数退出后可用。
看起来您似乎没有利用DistanceMatrix服务能够获取多个来源和目的地,并在一次通话中返回它们之间的行车距离。您发布的代码使用它的方式效率很低,一次只返回一个结果(如果您打算这样做,您也可以使用DirectionsService)。
答案 1 :(得分:0)
我认为这是做到这一点的方法:
var placesIndex = 0;
calculateDistance();
function calculateDistance(response,status){
if(response) {
var distancefield = distancefield;
if (status == google.maps.DistanceMatrixStatus.OK) {
var origins = response.originAddresses;
var place = places[placesIndex]; // use this in your calculations
var destinations = response.destinationAddresses;
var results = response.rows[0].elements;
for (var r = 0; r < results.length; r++) {
var element = results[r];
var distancetext = element.distance.text;
var durationtext = element.duration.text;
var to = destinations[r];
}//end for r
}//end if status=ok
}
if(placesIndex < places.length) {
var goto = new google.maps.LatLng(places[placesIndex].lat, places[placesIndex].lng);
service.getDistanceMatrix(
{
origins: [foundlatlng],
destinations: [goto],
travelMode: google.maps.TravelMode.WALKING,
unitSystem: google.maps.UnitSystem.IMPERIAL,
avoidHighways: false,
avoidTolls: false,
}, calculateDistance);//end service.getdistancematrix()
placesIndex ++;
}
}
答案 2 :(得分:0)
谢谢你们的建议!!!这是代码:
var service = new google.maps.DistanceMatrixService();//request distance matrix from the Googleplex
var outputdiv = document.getElementById('info');
var destArray = [<?php
$result = mysqli_query($mysqli, "SELECT * FROM places");
foreach($result as $key => $value){echo "'" . $value['address'] . "',"; };?>];
service.getDistanceMatrix(
{
origins: [foundlatlng],
destinations: destArray,
travelMode: google.maps.TravelMode.WALKING,
unitSystem: google.maps.UnitSystem.IMPERIAL,
avoidHighways: false,
avoidTolls: false,
}, callback);//end service.getdistancematrix()
function callback(response, status) {
if (status == google.maps.DistanceMatrixStatus.OK) {
var origins = response.originAddresses;
var destinations = response.destinationAddresses;
console.log(response);
for (var i = 0; i < origins.length; i++) {
var results = response.rows[i].elements;
for (var j = 0; j < results.length; j++) {
var element = results[j];
var distance = element.distance.text;
var duration = element.duration.text;
var from = origins[i];
var to = destinations[j];
generateArray(to, destinations, distance, duration);
}//end for j
}//end for i
}//end if
}//end callback
**var theindex = 1;
function generateArray(origin, destin, dis, dur){
places[theindex].distance = dis;
places[theindex].duration = dur;
theindex++;
}//end generateTable**
console.log(places);