I was instructed to use the Google Maps API to create a tool that a restaurant can use to determine delivery areas. Here is the progress: http://codepen.io/keithpickering/pen/NqdzKO
Users should be able to draw a polygon, after which it will quickly approach the nearby roads. This works relatively well, except for the fact that Google’s built-in function for working with the road ... well, sort of sucks. If your glasses are not close enough to each other, they will either refuse to snap to anything, or simply make some kind of weird damn line.
I need the tool to be more “forgiving” with its binding to the road; in other words, someone should lazily draw almost any kind 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 that I use for binding:
...
placeIdArray = [];
runSnapToRoad(poly, path, color);
});
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);
});
}
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);
}
}
Do I need to abandon the roads API and move on to a more personalized solution using the route service? Does anyone have any ideas? Thanks for any help.
source
share