Google地图:为原始路径计算的距离与为ElevationResult路径计算的距离之间的偏差 [英] Google maps: Discrepancy between distance calculated for original path and distance calculated for ElevationResult path

查看:141
本文介绍了Google地图:为原始路径计算的距离与为ElevationResult路径计算的距离之间的偏差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由几个LatLng组成的路径。
我使用下面的代码计算它的长度:

pre $ for(i = 0; i< polyPath.length- 1; i ++){
totalDistance2 = totalDistance2 +
google.maps.geometry.spherical.computeDistanceBetween(polyPath [i],polyPath [i + 1]);
}

现在我向ElevationService发送一个请求给此路径并计算

  for(i = 0; i< elevations.length-1; i ++){
totalDistance = totalDistance +
google.maps.geometry.spherical.computeDistanceBetween(elevation [i] .location,elevation [i + 1] .location);

$ / code>

对于我使用的任何路径,报告的两个距离之间总是有差异,原始路径距离总是较大(约1%)。

我使用512作为海拔请求的样本编号。降低这种距离会增加距离的差异。

编辑:路径包含大约200-300个顶点



edit2: jsfiddle的工作示例: http://jsfiddle.net/wz3pup5o/ 和相应的控制台输出:
初始路径长度:255
遍历路径上的距离latlngs:8334.314359218815
使用computelength(路径)的距离:8334.314359218815
在高程上迭代的距离结果:8199.077136099133

解决方案

高程点沿路径等距分布。通过连接这些点创建的路径需要一些快捷方式。检查这个小提琴中的蓝色多段线或下面的代码片段。特别是围绕着213/214和218/219点的急转弯

代码段:



 

html,body,#map {width:100 %;身高:100%; border:1px solid black;} < script src = https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js\"></script><script src =http://maps.googleapis.com/ maps / api / js?libraries = geometry>< / script>< div id =map>< / div>


I have a path consisting of several LatLngs. I calculate its length using the following code:

for (i = 0; i < polyPath.length-1; i++) {  
      totalDistance2 = totalDistance2 +   
         google.maps.geometry.spherical.computeDistanceBetween(polyPath[i],polyPath[i+1]);
}

Now i send a request to ElevationService for this path and compute the distance of the path provided by the result.

for (i = 0; i < elevations.length-1; i++) {
      totalDistance = totalDistance + 
         google.maps.geometry.spherical.computeDistanceBetween(elevations[i].location,elevations[i+1].location);
} 

For any path that i use there is always a discrepancy between the two distances reported, with the original path distance being always greater (by about 1%).

I have used 512 as the sample number for the elevation request. Lowering this increases the difference in distances.

edit: the path contains around 200-300 vertices

edit2: jsfiddle for working example: http://jsfiddle.net/wz3pup5o/ and the corresponding console output: initial path length: 255 distance iterating over path latlngs: 8334.314359218815 distance using computelength(path): 8334.314359218815 distance iterating over elevation results: 8199.077136099133

解决方案

The elevation points are equally spaced along the path. The path created by connecting those points takes some shortcuts. Examine the blue polyline in this fiddle or the code snippet below. Particularly around the sharp turns around point 213/214 and 218/219

code snippet:

var path;
var map;
$(window).load(function() {
  map = new google.maps.Map(document.getElementById('map'), {
    center: {
      lat: 20.0094671,
      lng: 73.3826942
    },
    zoom: 12,
    streetViewControl: false
  });

  var polyString = "iibyBody~L|MiOs@eAiAw@YYO]Mk@Ca@IkAHu@r@_C`@s@^c@dA_Ax@sAdB{Az@u@Xs@\Wf@I`BYVIRUvA_CX_@JWBg@Ga@Qe@i@]Wa@@{AFaAIe@KQKKGOCk@Cc@IWQs@@aAB{AH{ANmBPs@Zw@`@}@Di@Cw@Ae@HaAHuBF_@\aALa@b@g@JSBOAIQQ}@u@KUCg@?aANgAp@oBbAmB\gAPm@Dm@FgDK{@Pm@Vu@d@eCFy@T{APQn@[x@i@JYGa@kA}Cy@_Cu@aCIy@@QFMx@y@zAqB\{A`@o@d@g@f@w@HMYJgB`@gA\_CbAo@n@YHQ?i@SOKIMCY@kANs@`@eBb@uAf@{@FQEMKEoADy@BQVOnAIVSJS@a@Gy@OSMi@mAg@e@gB_Aq@i@s@s@}AcBoBmA}Ag@k@YQYK[UYu@Yg@E_@?UEa@Om@K_Ce@oA[{@[oDaBoDuAsAa@Y?MFGNWP[Iu@a@e@c@i@Mi@AYBSRMRE`@KXsA`AU@SGY]SKMHAX@LHR`@~@VLj@PR?TKNEF?DFDPER_@j@ERB`@JhA?l@G`@Kb@ORm@^]FYA]Ok@Qc@MgBOgBM]Ig@[q@_BKw@Ay@EU[Mm@_@U[AYZmAFe@CKIAODWd@Sp@MRMAIMB]HMD_@?{@CIGCQ@[V_A~@MDO?OKe@iAWq@UUk@OOMC]A}Aj@_CNWRQj@UfA?^?HMLs@\gBz@mCv@iCvA_CnA{BcBRgGj@w@Ro@FsBPo@Lk@PPn@";

  path = google.maps.geometry.encoding.decodePath(polyString);

  var polyline = new google.maps.Polyline({
    path: path,
    strokeColor: 'purple',
    map: map,
    strokeWeight: 4
  });

  var elevator = new google.maps.ElevationService();

  elevator.getElevationAlongPath({
    path: path,
    samples: 512
  }, calcdistance);

});

function calcdistance(results, status) {
  if (status == google.maps.ElevationStatus.OK) {

    var totalDistance2 = 0;
    console.log('initial path length:' + path.length);
    for (i = 0; i < path.length - 1; i++) {
      totalDistance2 = totalDistance2 + google.maps.geometry.spherical.computeDistanceBetween(path[i], path[i + 1]);
    }
    console.log('distance iterating over path latlngs:' + totalDistance2);
    console.log('distance using computelength(path):' + google.maps.geometry.spherical.computeLength(path));

    var totalDistance = 0;
    var epath = [];
    var old_loc = results[0].location;
    var bounds = new google.maps.LatLngBounds();
    var new_loc;
    console.log("results.length=" + results.length);
    for (i = 0; i < results.length; i++) {
      new_loc = results[i].location;
      epath.push(new_loc);
      bounds.extend(new_loc);
      var m = new google.maps.Marker({
        map: map,
        title: "" + i,
        position: new_loc,
        icon: {
          url: "https://maps.gstatic.com/intl/en_us/mapfiles/markers2/measle.png",
          size: new google.maps.Size(7, 7),
          anchor: new google.maps.Point(3.5, 3.5)
        }
      });
      totalDistance = totalDistance + google.maps.geometry.spherical.computeDistanceBetween(old_loc, new_loc);
      old_loc = new_loc;
    }
    map.fitBounds(bounds);
    var polyline = new google.maps.Polyline({
      path: epath,
      strokeColor: 'blue',
      map: map,
      strokeWeight: 1
    });
    console.log('distance iterating over elevation results:' + totalDistance);
    console.log('distance using computelength(epath):' + google.maps.geometry.spherical.computeLength(epath));
  }
}

html,
body,
#map {
  width: 100%;
  height: 100%;
  border: 1px solid black;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://maps.googleapis.com/maps/api/js?libraries=geometry"></script>
<div id="map"></div>

这篇关于Google地图:为原始路径计算的距离与为ElevationResult路径计算的距离之间的偏差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆