Link to home
Start Free TrialLog in
Avatar of Dodsworth
Dodsworth

asked on

RouteQuery Mapping Question

I have some code for generating a route on a map.  It uses RouteQuery.QueryAsync and a QueryCompleted handler to get at the results.

Problem is that I want to get multiple and be able to link the strRouteName in getRoute when the route is returned.

 Private Sub getRoute(strRouteName As String, gStart As GeoCoordinate, gEnd As GeoCoordinate)
                    Dim mRouteQuery As New RouteQuery()
                    Dim mRouteCoordinates As New List(Of GeoCoordinate)()                       
                    mRouteCoordinates.Add(New GeoCoordinate(gStart))
                    mRouteCoordinates.Add(New GeoCoordinate(gEnd))
                    mRouteQuery.Waypoints = mRouteCoordinates
                    AddHandler mRouteQuery.QueryCompleted, AddressOf mRouteQuery_QueryCompleted
                    mRouteQuery.QueryAsync()
                    mGeocodeQuery.Dispose()
    End Sub

    Private Sub mSearchGeocodeQuery_QueryCompleted(sender As Object, e As QueryCompletedEventArgs(Of IList(Of MapLocation)))
            If e.[Error] Is Nothing Then
                showresults(e.Result)
            End If
            mGeocodeQuery.Dispose()
    End Sub

Open in new window

Avatar of Bob Learned
Bob Learned
Flag of United States of America image

It might help if you told us what type of application/platform that you are working, so that we don't have to guess...
If this is what you are talking about

Finding and Mapping a Route in Windows Phone 8
http://blogs.msdn.com/b/matthiasshapiro/archive/2013/06/14/finding-and-mapping-a-route-in-windows-phone-8.aspx

I would think that you could add multiple waypoints, and get the name from the MapLocation.Information.Name property.
Avatar of Dodsworth
Dodsworth

ASKER

Sorry.. It's Windows Phone 8.

I'm trying to calculate driving distances from a number of points to a given point, so I think that I really need to call the query a number of times.
You should be able to add multiple waypoints, and get the distance between the GeoCoordinates, using the GetDistanceTo method.  The MapLocation has the GeoCoordinate property that should give you that information.
But doesn't GetDistanceTo return an 'as the crow flies' distance ?  I need driving distance.
I always assumed traveling distance, but that is not a correct assumption, and I am having difficulty finding a reference to explain the meaning...(d'oh)
now I AM confused :)
I found this reference that suggests that Route.LengthInMeters is what we are looking for.

Guide to the Windows Phone 8 Maps API
http://developer.nokia.com/community/wiki/What's_new_in_Windows_Phone_8#Maps

void query_QueryCompleted(object sender, QueryCompletedEventArgs<Route> e)
{
    myMap.AddRoute(new MapRoute(e.Result));
    StringBuilder sb = new StringBuilder();
    sb.AppendLine("Distance to destination: " + e.Result.LengthInMeters);
    sb.AppendLine("Time to destination: " + e.Result.EstimatedDuration);
    foreach (var maneuver in e.Result.Legs.SelectMany(l => l.Maneuvers))
    {
        sb.AppendLine("At " + maneuver.StartGeoCoordinate + " " +
                                maneuver.InstructionKind + ": " +
                                maneuver.InstructionText + " for " +
                                maneuver.LengthInMeters + " meters");
    }
     MessageBox.Show(sb.ToString());
}

Open in new window

I believe that will tell me the distance between two waypoints (a leg), but it won't tell me which leg corresponds to which two waypoints?
I believe that you can get that information from the maneuver:

       maneuver.StartGeoCoordinate

and the distance:

       maneuver.LengthInMeters
So I would be looking for the first and last maneuver of a leg ?
But is the last maneuver the endpoint of the route or the geoocordinate of the place where you'd change direction prior to reaching your destination ?
Those are all good questions.  Here is my attempt to find a good reference to verify the correct answer.

Map Explorer
http://developer.nokia.com/resources/library/Lumia/code-examples/map-explorer.html

RouteQuery can be used to obtain a route from one geographic point to another. Route query is launched from the GeocodeQuery_QueryCompleted event handler while searching for route. This example uses only two way points for a route, beginning and destination. The result of the RouteQuery is a Route. MapRoute is a visual representation of the Route that can be readily applied to the Map control. Route is divided to RouteLegs between two way points. Each RouteLeg has a collection of RouteManeuvers representing the actions to be taken along the path of a route leg.
*long wail for help
Windows 8 Phone Map Service is not a top area of expertise for me, so if you are hungry for a solution, then you should request attention for this question.

I am always willing to help someone, and learn something new at the same time.

I downloaded the Nokia sample code for the Map Explorer, so that I can make sure that I understand how the map services work.
I just discovered some info ..

Ok. It looks like  RouteQuery.QueryAsync is a legacy use of the "Async" nomenclature and doesn't actually return a Task to use the async system.

For this case you can loop through your set of RouteQueries, set the QueryCompleted handler for each, and then call QueryAsync.

Since QueryAsync doesn't set the tokenId you can differentiate the calls by providing different QueryCompleted handlers. One method is to create an object which contains the delegate and an identifier for the route the Query is part of. The QueryCompleted handler can then check the identifier on its object.

Can you help with the last two sentences ?
I was unable to install Windows 8.1, and the Windows 8 Phone Emulator, since my hardware doesn't support SLAT (Second Level Application Translation).  

I should be able to help you with your last comment, unless you figured it out yourself.
please :)
From this conversation, I came up with this sample code:

using System;
using System.Diagnostics;
using System.Web.Routing;

namespace Sample
{
    public class QueryWithId
    {
        public string QueryId { get; set; }

        public void RouteQueryCompletedHandler(object sender, QueryCompletedEventArgs<Route> e)
        {
            Debug.WriteLine("QueryCompleted {0}");
        }

    }

    public class QueryCompletedEventArgs<TType> : EventArgs
    {
    }
}

Open in new window


Example showing how to call:

            var routeQuery = new RouteQuery();
            var qid = new QueryWithId() { QueryId = "1" };
            routeQuery.QueryCompleted += qid.RouteQueryCompletedHandler;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That works great thanks