繁体   English   中英

传递带有Google Maps Distance Matrix请求的标识符

[英]Passing an Identifier with a Google Maps Distance Matrix Request

我正在尝试使用Google的距离矩阵服务来计算A点到B点之间的行驶距离,然后计算B点到C点之间的行驶距离,依此类推。

我意识到矩阵系统旨在确定一组不同的结果,但是我的目的是连续发出多个单独的请求。 但是,当我发出请求时,异步性质意味着返回请求时,无法知道它是哪个请求(从A点到B点或从B点到C点),特别是因为它重新格式化了最初给它的地址,因此即使保留一系列请求也无济于事,因为发送的地址与收到的地址不匹配。

所以我的问题是,是否可以将标识符与请求一起传递,以便您可以跟踪哪个请求对应于哪个地址? 我尝试了以下方法:

var service = new google.maps.DistanceMatrixService();
            service.getDistanceMatrix(
            {
                origins: [address1],
                destinations: [address2],
                travelMode: google.maps.TravelMode.DRIVING,
                unitSystem: google.maps.UnitSystem.METRIC,
                uniqueIdentifier: 1
            }, someOtherName);

但是API不喜欢传递“ uniqueIdentifier”字段。

谢谢!

不,你不能那样做。 但是,您可以做的是在返回的results对象上推送一个标识符。 在此示例中,我将代码分为一个函数和对该函数的调用,将标识符作为函数的参数传递,将其添加到结果对象,然后从传递给回调的数据中挑选出来。

function getDistance(identifier, callback) {
  var service = new google.maps.DistanceMatrixService();
  service.getDistanceMatrix({
    origins: [address1],
    destinations: [address2],
    travelMode: google.maps.TravelMode.DRIVING,
    unitSystem: google.maps.UnitSystem.METRIC
  }, function (results) {
     results.identifier = identifier;
     callback(results);
  });
}

var identifier = 1;
getDistance(identifier, function (results) {
  console.log(results.identifier); // 1
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM