确定 GPS 坐标是否落在道路上
Determine if GPS coordinates fall on a road
我试图通过 Python
脚本确定一个点是在 road/highway/freeway 等上还是在建筑物中。对于某些输入,它们将包含用户的速度,这意味着在某些情况下,考虑到设备速度,这将是显而易见的。但是,例如,当用户的瞬时速度较小时,可能是由于交通拥堵、红灯时停止等。
我希望能够输入一对输入,例如。 -33.852592, 151.210663
并接收关于用户坐标是否落在道路上的布尔结果。
我遇到过 Google places
和 OSRM
但还没有找到解决方案。我还缺少其他选项吗?
一个选项是执行 DirectionsService.route 请求,并将 origin
和 destination
设置为输入点。这将 return 从道路上最近的位置开始的 0 长度路线(在合理范围内)。如果没有结果,则该点不在路上。如果得到结果,您可以计算输入与 returned 点之间的距离,以便就坐标是否在路上做出明智的决定。
请注意,GPS 设备本身可能不那么准确。
proof of concept fiddle (with your example point)
代码片段:
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
脚本确定一个点是在 road/highway/freeway 等上还是在建筑物中。对于某些输入,它们将包含用户的速度,这意味着在某些情况下,考虑到设备速度,这将是显而易见的。但是,例如,当用户的瞬时速度较小时,可能是由于交通拥堵、红灯时停止等。
我希望能够输入一对输入,例如。 -33.852592, 151.210663
并接收关于用户坐标是否落在道路上的布尔结果。
我遇到过 Google places
和 OSRM
但还没有找到解决方案。我还缺少其他选项吗?
一个选项是执行 DirectionsService.route 请求,并将 origin
和 destination
设置为输入点。这将 return 从道路上最近的位置开始的 0 长度路线(在合理范围内)。如果没有结果,则该点不在路上。如果得到结果,您可以计算输入与 returned 点之间的距离,以便就坐标是否在路上做出明智的决定。
请注意,GPS 设备本身可能不那么准确。
proof of concept fiddle (with your example point)
代码片段:
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>