Reputation: 786
I've been tasked with using the Google Maps API to create a tool which a restaurant can use to define delivery zones. Here is the progress: http://codepen.io/keithpickering/pen/NqdzKO
Users should be able to draw a polygon, after which it will snap to nearby roads for accuracy. This is working relatively well, except for the fact that Google's built-in snap to road functionality...well, sort of sucks. If your points aren't close enough together, it will either refuse to snap to anything, or just make some weird janky line.
What I need is for the tool to be more "forgiving" with its road snapping; in other words, someone should be able to lazily draw pretty much any sort of polygon from any zoom distance, and the lines should be FORCED to snap to one road or another.
Here is the part of the code I'm using to snap:
...
// Snap polygon to roads
placeIdArray = [];
runSnapToRoad(poly, path, color);
});
// Snap a user-created polyline to roads and draw the snapped path
function runSnapToRoad(poly, path, color) {
var pathValues = [];
for (var i = 0; i < path.getLength(); i++) {
pathValues.push(path.getAt(i).toUrlValue());
}
$.get('https://roads.googleapis.com/v1/snapToRoads', {
interpolate: true,
key: apiKey,
path: pathValues.join('|')
}, function(data) {
processSnapToRoadResponse(data);
drawSnappedPolyline(poly, path, color);
});
}
// Store snapped polyline returned by the snap-to-road method.
function processSnapToRoadResponse(data) {
snappedCoordinates = [];
placeIdArray = [];
for (var i = 0; i < data.snappedPoints.length; i++) {
var latlng = new google.maps.LatLng(
data.snappedPoints[i].location.latitude,
data.snappedPoints[i].location.longitude);
snappedCoordinates.push(latlng);
placeIdArray.push(data.snappedPoints[i].placeId);
}
}
Will it be necessary to give up on the Roads API and go for a more customized solution using the directions service? Does anyone have any ideas? Thanks for any and all help.
Upvotes: 4
Views: 8112
Reputation: 21
I think you can try from Microsoft Snap points to road api. It provides very high accuracy up to 1000 points and GPS points can be up to 2.5 kilometer of each other.
Upvotes: 2
Reputation: 8190
Unfortunately, that cannot be done with the current API. The API is designed to correct paths that actually traversed roads and takes into consideration factors like permitted traffic directions (i.e. one-way roads), valid turns and the ordering of the provided points.
A freehand drawing of a path near some roads may not be a valid route (especially from a distant zoom level), so the API is not finding a path with a high probability of being real.
If you like, you can request this feature on the issue tracker.
Upvotes: 0