【问题标题】:Google Maps. Tracking history on the map. Incorrect route because of errors in the coordinates谷歌地图。在地图上追踪历史。由于坐标错误,路线不正确
【发布时间】:2017-07-20 09:00:44
【问题描述】:

我的应用会在 Google 地图上显示车辆的跟踪历史记录。我们每 2 分钟从我们的移动应用程序接收一次位置并将其存储在数据库中。之后,我们在地图上显示路线(使用 JS API,Directions Service)。

{
  origin: 'Chicago, IL',
  destination: 'Los Angeles, CA',
  waypoints: [
    ...
     ],
  provideRouteAlternatives: false,
  travelMode: 'DRIVING',
  unitSystem: google.maps.UnitSystem.IMPERIAL
}

但是,有时位置可能不准确,只有 3-5 米。例如,某些位置可以显示在迎面而来的交通线上。结果,谷歌围绕几个街区等创建了路线。

所以问题是:有没有办法忽略这些不准确的点,只是“强迫”谷歌在一个方向上创建路线?

谢谢!

【问题讨论】:

  • 我相信出于资产跟踪目的,Roads API 可能比 Directions API 更合适。但是,Roads API 中使用的捕捉算法假设 GPS 点非常接近(小于 400 m)。间隔 2 分钟,我担心点会很稀疏,插值算法不能很好地工作。
  • 似乎应该有所帮助。我会尽力。据我了解,除了 Directions API 之外,我还可以使用此 API。所以我可以用 Roads API“纠正”这些点并用 Directions API 绘制路线。
  • 没有帮助 :( 似乎它仅适用于点彼此太近的情况。在我的情况下,两点之间可能有 2-10 英里。

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


【解决方案1】:

我有个主意。看起来它有效,至少对于我在类似情况下选择的航点而言。

我将腿(= 航点之间的路线段)放在一个 for 循环中。对于每个部分,我计算/读取“沿路线的距离”并将其除以飞行路径距离。这个比率应该是 1 左右。如果它远大于 1,则有问题。我将阈值设置为 2.0,您可能应该降低它(估计一个介于 1 和 2 之间的值)。

找到这样的因素后,我删除了那个航路点(好吧,我创建了一个没有这个项目的新数组“newWaypoints”),然后重新开始,这会在 2 秒 setTimeout 后生成一条红色路线。

函数是递归的,我不知道应该删除的多个点会发生什么。也许它可能会再试一次以上。

<style>
  #map {height: 400px;}
</style>
<div id="map"></div>
<div id="log"></div>
<input type="button" value="click" onclick="calc()">
<script>
var directionsDisplay;
var directionsService;
var map;
var maxFactor = 2.0;    // FEEL FREE TO GUESTIMATE ANOTHER VALUE
function initMap() {
  var start = new google.maps.LatLng(50.96622130043278,3.8518730520809185);
  var mapOptions = {
    zoom:7,
    center: start
  }
  map = new google.maps.Map(document.getElementById('map'), mapOptions);
  directionsService = new google.maps.DirectionsService();
}
function calcRoute(start, end, waypoints, color) {
  directionsDisplay = new google.maps.DirectionsRenderer({ map: map, polylineOptions: {strokeColor: color} });
  var request = {
    origin: start,
    destination: end,
    waypoints: waypoints,
    provideRouteAlternatives: false,
    travelMode: 'DRIVING',
    unitSystem: google.maps.UnitSystem.IMPERIAL
  };
  directionsService.route(request, function(result, status) {
    if (status == 'OK') {
      var newWaypoints = [];

      directionsDisplay.setDirections(result);
      var legs = result.routes[0].legs;
      var problemIndex = -1;
      for(var i in legs) {
        var routeSegment = legs[i].distance.value;
        var origin = legs[i].start_location;
        var destination = legs[i].end_location;
        var flightpathDistance = google.maps.geometry.spherical.computeDistanceBetween(origin, destination);
        var factor = (routeSegment / flightpathDistance);
        if(factor > maxFactor && problemIndex == -1) {
          // too long
          problemIndex = i;
          document.getElementById('log').innerHTML += 'factor ' + factor.toFixed(2) + ' - segment looks too long. romove waypoint ' + i + ' (0 based)<br/>'; 
        }
        else if(factor > maxFactor && problemIndex == i -1) {
          if(i<legs.length - 1) {  // skip the destination; this is not a waypoint
            newWaypoints.push(waypoints[i]);
          }
          document.getElementById('log').innerHTML += 'factor ' + factor.toFixed(2) + ' - segment also too long, but the problem is probably the previous segment<br/>'; 
        }
        else {
          if(i<legs.length - 1) {  // skip the destination; this is not a waypoint
            newWaypoints.push(waypoints[i]);
          }
          document.getElementById('log').innerHTML += 'factor ' + factor.toFixed(2) + '<br/>';
        }
      }
      document.getElementById('log').innerHTML += '<hr/>';
      if(problemIndex > -1) {
        setTimeout(function() {
          calcRoute(start, end, newWaypoints, 'red');
        }, 2000);

      }
    }
  });      
}
function calc() {
  calcRoute(
    '50.95487921891042,3.879781222422025', 
    '51.002379049703315,3.757394492495223',
    [
      {location: '50.96622130043278,3.8518730520809185', stopover: true},
      {location: '50.9725522849737,3.8379914402503345', stopover: true},
      {location: '50.97957292616706,3.8236401199901593', stopover: true},  // This is the problem point, on the opposite side of the road
      {location: '50.98570531465853,3.81125807762146', stopover: true},
      {location: '50.98941308813775,3.8019619700935436', stopover: true}
    ],
    "blue"
  );
}
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap&libraries=geometry"></script>

【讨论】:

  • 据我了解,只有在“不正确”的腿形成“圆圈”的情况下才会有所帮助。但是,如果错误的腿根据“因素”看起来正常但方向不正确,该怎么办?
  • 你试过了吗?您能否给出一组显示问题的坐标(不是大循环,而是更微妙的错误);所以我有目标。我看看能不能想出更好的技术
  • 我没试过。我担心它会过滤很多有用的点。例如:link
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-14
  • 1970-01-01
  • 2013-04-30
  • 1970-01-01
  • 1970-01-01
  • 2012-07-23
  • 2021-05-02
相关资源
最近更新 更多