Can I set a polygon as the destination in my directions? RRS feed

  • Question

  • I have a bordered area (aka polygon) that I want to be able to get directions to from all around it. That is, I would like to find the best route to the point of the polygon nearest to my current location... I have the "from" waypoint set to my current location, but I'm struggling to set the "to" point as a polygon. Any ideas out there? Have a look at what I've got set up. (I cut out most of the polygon for simplicity's sake) Thanks :)

    var location1 = new Microsoft.Maps.Location(14.042756, 99.586714);
    var location2 = new Microsoft.Maps.Location(14.043338, 99.586553);
    var location3 = new Microsoft.Maps.Location(14.045163, 99.586371);
    var location4 = new Microsoft.Maps.Location(14.052919, 99.586465);
                // Create a polygon 
                var vertices = new Array(location1, location2, location3, location4, location1);
    				//Request the user's location
            navigator.geolocation.getCurrentPosition(function (position) {
                var loc = new Microsoft.Maps.Location(
    			Microsoft.Maps.loadModule('Microsoft.Maps.Directions', function () {
                        var directionsManager = new Microsoft.Maps.Directions.DirectionsManager(map);
                        // Set Route Mode to driving
                        directionsManager.setRequestOptions({ routeMode: Microsoft.Maps.Directions.RouteMode.driving });
                        var waypoint1 = new Microsoft.Maps.Directions.Waypoint({ address: 'Your Location', location: new Microsoft.Maps.Location(position.coords.latitude, position.coords.longitude) });
                        var waypoint2 = new Microsoft.Maps.Directions.Waypoint({ address: 'Territory', location: new Microsoft.Maps.Location(14.042756, 99.586714) });
                        // Set the element in which the itinerary will be rendered
                        directionsManager.setRenderOptions({ itineraryContainer: document.getElementById('printoutPanel') });

    • Edited by bnunya Tuesday, May 24, 2016 4:44 PM
    Tuesday, May 24, 2016 4:43 PM


  • The directions module does not support polygons as waypoints. You can however still accomplish this with a bit more work. One option is to use the Spatial Math module and loop through each location in the polygon and then calculate the distance to the from location. From this you can find the closest location in the polygon. This will of course only work with the locations that make up the polygon and will not grab a location on the edge of a polygon. In a few months the geometry functions for the spatial math library should be released. In there is a function to calculate the closest point on the edge of a polygon.

    [Blog] [twitter] [LinkedIn]

    Tuesday, May 24, 2016 7:10 PM