简体   繁体   English

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

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

I'm trying to use Google's Distance Matrix Service to calculate the driving distance between Point A to Point B, then Point B to Point C and so on. 我正在尝试使用Google的距离矩阵服务来计算A点到B点之间的行驶距离,然后计算B点到C点之间的行驶距离,依此类推。

I realise the matrix system is designed to determine a different set of results, but it's my intention to make multiple separate requests consecutively. 我意识到矩阵系统旨在确定一组不同的结果,但是我的目的是连续发出多个单独的请求。 However, when I make a request, the asynchronous nature means that when it is returned, there is no way of knowing which request it was (Point A to B or Point B to C), especially as it reformats the address you give it initially, so even keeping an array of requests doesn't help, as the addresses sent don't match the ones received. 但是,当我发出请求时,异步性质意味着返回请求时,无法知道它是哪个请求(从A点到B点或从B点到C点),特别是因为它重新格式化了最初给它的地址,因此即使保留一系列请求也无济于事,因为发送的地址与收到的地址不匹配。

So my question is, is it possible to pass an identifier along with the request, so that you can keep track of which request corresponds to which address? 所以我的问题是,是否可以将标识符与请求一起传递,以便您可以跟踪哪个请求对应于哪个地址? I have tried the following: 我尝试了以下方法:

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);

But the API doesn't like the "uniqueIdentifier" field being passed. 但是API不喜欢传递“ uniqueIdentifier”字段。

Thanks! 谢谢!

No. You can't do that. 不,你不能那样做。 However, what you can do is push an identifier on the returned results object instead. 但是,您可以做的是在返回的results对象上推送一个标识符。 In this example I've separated the code into a function and a call to that function, passing in an identifier as a parameter to the function, adding it to the results object, and picking it out of the data that's passed to the callback. 在此示例中,我将代码分为一个函数和对该函数的调用,将标识符作为函数的参数传递,将其添加到结果对象,然后从传递给回调的数据中挑选出来。

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