我有一个应用程序接收有限的航路点数据,尽管 Google Maps Road Snapping 在根据该数据猜测正确道路方面做得非常出色,但我仍然遇到不准确的程度,这对我们的应用程序不起作用.
在此示例中,紫色标记表示正在发送的真实世界“源”航点,而蓝色标记显示根据源航点的数据从 Google 返回的捕捉的航点数据。我正在使用捕捉的蓝色航点生成一条折线来显示路线(紫色折线),不幸的是,捕捉的航点和后续路线应该看起来更像红色折线。
我已经在 API 的在线演示中手动测试了这一点,其中包含类似的“源”航点,并且路线仍然捕捉到不正确的路径,所以我只能假设根本没有足够的数据让 Google 准确捕捉。问题是,鉴于这样的有限数据,有什么方法可以提高正确捕捉的几率?有没有办法我可以插入有限的源航点来尝试“引导”谷歌提供更准确的快照?
这是类似于我正在使用的代码 - JSFiddle 在这里
//setup vars
var trip = [{
"lat": -27.068,
"lng": 153.1483
}, {
"lat": -27.0642,
"lng": 153.1546
}, {
"lat": -27.0552,
"lng": 153.156
}, {
"lat": -27.0518,
"lng": 153.1563
}, {
"lat": -27.0503,
"lng": 153.1552
}, {
"lat": -27.0457,
"lng": 153.1456
}, {
"lat": -27.042,
"lng": 153.1463
}, {
"lat": -27.0349,
"lng": 153.1476
}];
var unsnappedWaypoints = [];
var snappedWaypoints = [];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 14,
center: {
lat: 0,
lng: 0
}
});
var bounds = new google.maps.LatLngBounds();
//add each waypoint to an array of lat/lngs
$.each(trip, function(key, waypoint) {
unsnappedWaypoints.push(waypoint.lat + ',' + waypoint.lng);
var marker = new google.maps.Marker({
map: map,
icon: 'http://mt.google.com/vt/icon/name=icons/spotlight/spotlight-ad.png',
position: {
lat: waypoint.lat,
lng: waypoint.lng
}
});
});
//perform Google Maps API call with joined array for snapped results
$.ajax({
url: 'https://roads.googleapis.com/v1/snapToRoads?path=' + unsnappedWaypoints.join('|') + '&key=AIzaSyA1JWR3ohBFQ_P7F5eSMSJb0dwV9PbB3pA&interpolate=true',
crossDomain: true,
dataType: 'jsonp'
}).done(function(response) {
//iterate through returned waypoints to create array of lat/lngs for polyline
$.each(response, function(key, snappedPoints) {
$.each(snappedPoints, function(key, snappedPoint) {
snappedWaypoints.push({
lat: snappedPoint.location.latitude,
lng: snappedPoint.location.longitude
});
//add snapped waypoints to map to show difference between originals and returned
var marker = new google.maps.Marker({
map: map,
icon: 'http://mt.google.com/vt/icon?color=ff004C13&name=icons/spotlight/spotlight-waypoint-blue.png',
position: {
lat: snappedPoint.location.latitude,
lng: snappedPoint.location.longitude
}
});
//increase the bounds to take into account waypoints
bounds.extend(new google.maps.LatLng(snappedPoint.location.latitude, snappedPoint.location.longitude));
});
});
//create polyline from snapped waypoints
var tripRoute = new google.maps.Polyline({
path: snappedWaypoints,
gseodesic: true,
strokeColor: '#663496',
strokeOpacity: 1.0,
strokeWeight: 2
});
tripRoute.setMap(map);
//fit these bounds to the map
map.fitBounds(bounds);
});