简体   繁体   English

使用google api测量3个标记之间的距离

[英]Measure distance between 3 markers using google api

I am using react js and I am getting coordinates from user input.我正在使用 react js,我正在从用户输入中获取坐标。 Now I want to calculate the total distance in miles between those coordinates.现在我想计算这些坐标之间的总距离(以英里为单位)。 eg 51.5035914, -0.1193578 51.5031346, -0.1193844 51.5038217, -0.1282733例如 51.5035914, -0.1193578 51.5031346, -0.1193844 51.5038217, -0.1282733

So the distance from point a to b + point b to c.所以a点到b点的距离+b点到c点的距离。

PS.附注。 In above example I added only 3 points but it can be 30 or more在上面的例子中,我只加了 3 点,但它可以是 30 或更多

If you want to get the distance and time duration to travel between your coordinates, you can use Google Maps Platform API's Distance Matrix API .如果您想获得在您的坐标之间旅行的距离和持续时间,您可以使用Google Maps Platform API 的Distance Matrix API

To do this, you can get the distance/duration between point A and point B in one Distance Matrix request first and store it in a variable.为此,您可以首先在一个距离矩阵请求中获取点 A 和点 B 之间的距离/持续时间,并将其存储在一个变量中。 Then get the distance/duration between point B and point C in another request and add it in the same variable too.然后在另一个请求中获取点 B 和点 C 之间的距离/持续时间并将其添加到同一变量中。

Here is a snippet of a code that shows the Distance Matrix request:这是显示距离矩阵请求的代码片段:

    var service = new google.maps.DistanceMatrixService;
    var orig = this.state.input;
    var dest = this.state.destination;

    service.getDistanceMatrix({
          origins: [orig],
          destinations: [dest],
          travelMode: 'DRIVING',
          unitSystem: google.maps.UnitSystem.METRIC,
          avoidHighways: false,
          avoidTolls: false
        }, (response, status) => {
          if (status !== 'OK') { 
            alert('Error was: ' + status);
          } else {
            var origins = response.originAddresses;
            var destinations = response.destinationAddresses;

            //Loop through the elements row to get the value of duration and distance
            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 distanceString = element.distance.text;
              var durationString = element.duration.text;

              //add new value to the previousValue 
              this.setState ({
                distance: this.state.distance + parseInt(distanceString, 10)
                });
              console.log(this.state.distance);
              this.setState ({
                duration: this.state.duration + parseInt(durationString, 10)
                });
                console.log(this.state.duration);
               }
             }
          }
          });

Hope this helps!希望这可以帮助!

I am using react js and I am getting coordinates from user input.我正在使用react js,并且正在从用户输入中获取坐标。 Now I want to calculate the total distance in miles between those coordinates.现在,我想计算这些坐标之间的总距离(以英里为单位)。 eg 51.5035914, -0.1193578 51.5031346, -0.1193844 51.5038217, -0.1282733例如51.5035914,-0.1193578 51.5031346,-0.1193844 51.5038217,-0.1282733

So the distance from point a to b + point b to c.因此,从点a到b的距离+点b到c的距离。

PS. PS。 In above example I added only 3 points but it can be 30 or more在上面的示例中,我仅添加了3分,但可以是30分或更多

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

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