Google地图:为原始路径计算的距离与为ElevationResult路径计算的距离之间的差异

时间:2015-11-09 07:45:28

标签: google-maps google-maps-api-3

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

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(elevations[i].location,elevations[i+1].location);
} 

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

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

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

edit2:jsfiddle for working example:http://jsfiddle.net/wz3pup5o/和相应的控制台输出: 初始路径长度:255 迭代路径latlngs的距离:8334.314359218815 使用computelength(path)的距离:8334.314359218815 迭代高程结果的距离:8199.077136099133

1 个答案:

答案 0 :(得分:0)

高程点沿路径等距离。通过连接这些点创建的路径需要一些快捷方式。检查this fiddle中的蓝色折线或下面的代码段。特别是在点213/214和218/219周围的急转弯附近

代码段

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>