python - 确定 GPS 坐标是否落在道路上

标签 python google-maps gps google-places-api osrm

我试图通过Python脚本确定一个点是在道路/高速公路/高速公路等上还是在建筑物中。对于某些输入,它们将包含用户的速度,这意味着在某些情况下,考虑到设备速度,这将是显而易见的。然而,例如,当用户的瞬时速度很小时,可能是由于交通、红绿灯等原因造成的。

我希望能够输入一对输入,例如。 -33.852592, 151.210663 并接收关于用户坐标是否落在道路上的 bool 结果。

我遇到过 Google 地点OSRM,但尚未找到解决方案。我还缺少其他选项吗?

最佳答案

一种选择是执行 DirectionsService.route 请求,并将 origindestination 设置为输入点。这将从道路上最近的位置返回 0 长度的路线(在合理范围内)。如果没有结果,则说明不在路上。如果获得结果,您可以计算输入与返回点之间的距离,以便就坐标是否在道路上做出明智的决定。

请注意,GPS 设备本身可能并不那么准确。

proof of concept fiddle (with your example point)

screenshot of map showing result

代码片段:

var geocoder;
var map;

function initialize() {
  var testPoint = {
    lat: -33.852592,
    lng: 151.210663
  };
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: testPoint,
      zoom: 22,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  var marker = new google.maps.Marker({
    position: testPoint,
    map: map,
    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)
    }
  });

  var directionsService = new google.maps.DirectionsService();
  directionsService.route({
    origin: testPoint,
    destination: testPoint,
    travelMode: "DRIVING"
  }, function(result, status) {
    if (status == 'OK') {
      var marker = new google.maps.Marker({
        position: result.routes[0].legs[0].steps[0].start_location,
        map: map,
        icon: {
          url: "https://maps.gstatic.com/intl/en_us/mapfiles/markers2/measle_blue.png",
          size: new google.maps.Size(7, 7),
          anchor: new google.maps.Point(3.5, 3.5)
        }
      });
      var distance = google.maps.geometry.spherical.computeDistanceBetween(result.routes[0].legs[0].steps[0].start_location, marker.getPosition());
      if (distance < 10)
        document.getElementById('info').innerHTML = "distance=" + distance + "m on road";
      else
        document.getElementById('info').innerHTML = "distance=" + distance + "m not on road";
    } else alert("status=" + status);
  });
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}

#map_canvas {
  height: 90%;
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry"></script>
<div id="info"></div>
<div id="map_canvas"></div>

关于python - 确定 GPS 坐标是否落在道路上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53569834/

相关文章:

python - 在多个时间范围内聚合/重采样 pandas 多索引数据帧并预测 ARIMA

python - 将列表附加到数据框

python - 如何将字符串中的文字转义序列转换为相应的字节?

android - 如何解决 "minTime in Android 2.3.4 not working"错误

android - 哪个是更高的准确度标准 : ACCURACY_HIGH or ACCURACY_FINE?

python - 对曲线上定期采样的 2D 点列表进行排序

执行本地商业搜索的 iPhone Mapkit 应用程序

ios - 如何解决在 Xcode 中使用 Google Maps SDK for iOS 时出现的错误 "Thread 1: SIGABRT error"

android - map 工具栏始终以 liteMode 显示

android获取当前位置