Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Google Maps API: Making 'snap to road' more forgiving

  • This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #203260
    keithpickering
    Participant

    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.

Viewing 1 post (of 1 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.