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

时间:2018-12-01 10:22:48

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

我正在尝试通过Python脚本确定某个点是在道路/高速公路/高速公路等上还是在建筑物中。对于某些输入,它们将包含用户的速度,这意味着在某些情况下,给定设备速度,这将是显而易见的。但是,例如,在用户的瞬时速度较小的情况下,这可能是由于交通拥堵,在灯光下停车等引起的。

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

我遇到过Google placesOSRM,但还没有找到解决方案。我还有其他选择吗?

1 个答案:

答案 0 :(得分:2)

一个选项是通过将origindestination设置为输入点来执行DirectionsService.route请求。这将从道路上最近的位置返回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>