The RouteBoxer class generates a set of LatLngBounds
objects
that are guaranteed to cover every point within a specified distance of a path, such as that generated for a route by the
DirectionsService
class.
The primary use case for this class is to support implementing Search along a route against a Spatial db that supports bounding box queries.
Note that it is not the intention of the class to limit the bounding boxes to only those regions within the specified distance of the route, which is not possible given that routes are not rectangular. Consequently searches performed using these bounding boxes may return results up to ~3x the given distance from the route (twice the distance across the diagonal of a square with sides of the given distance).
To use the RouteBoxer class, first load the RouteBoxer.js file into your page:
<script type="text/javascript" src="../../routeboxer/src/RouteBoxer.js"></script>
Then create a RouteBoxer
object, and pass an array of
google.maps.LatLng
objects and a distance to the RouteBoxer.box()
method.
If implementing search along a route the overview_polyline
property of a
google.maps.DirectionsRoute
object contains a suitable array of LatLng
objects:
var directionService = new google.maps.DirectionsService(); var rboxer = new RouteBoxer(); var distance = 20; // km directionService.route(request, function(result, status) { if (status == google.maps.DirectionsStatus.OK) { // Box the overview path of the first route var path = result.routes[0].overview_path; var boxes = routeBoxer.box(path, distance); for (var i = 0; i < boxes.length; i++) { var bounds = box[i]; // Perform search over this bounds } } });
To visualise the boxes generated for a given route see the example.
The RouteBoxer class generates the boxes for a route in a number of steps.