Routing: Public Transport

Topics: Feature Requests
Nov 14, 2012 at 2:55 PM

Hey radioman!

Thank your very much for greatmaps.

Is it possible to add the routing option public transport for googleProvider? It could be very useful.

I also found this thread about routing directions (http://greatmaps.codeplex.com/discussions/241332)

In fact this would be even more handy if it would be able to get the public transport directions instead of my routing request.

 

Keep up the good work and support!

Coordinator
Nov 14, 2012 at 3:19 PM

any chance to provide some working code? ;}

Nov 14, 2012 at 3:29 PM
Edited Nov 14, 2012 at 3:36 PM

I'm sorry, I am an amateur programer.

I am happy I got everything working so far xD Only using your built-in methods and functions

I only saw this code snippet from you (haven't used it in my program yet):

GDirections s;
var x = GMapProviders.GoogleMap.GetDirections(out s, "Lithuania,Vilnius", "Lithuania,Kaunas", false, false, false, true);
if(x == DirectionsStatusCode.OK)
{
   Debug.WriteLine(s.Summary + ", " + s.Copyrights);
   Debug.WriteLine(s.StartAddress + " -> " + s.EndAddress);
   Debug.WriteLine(s.Distance);
   Debug.WriteLine(s.Duration);
   foreach(var step in s.Steps)
   {
     Debug.WriteLine(step);  
   }
}

I thought you could add another parameter for "use public transport" == true, and in code-behind change it appropriatly? 
I really don't know how much effort it is. Maybe only changing the API request to google?

Nov 15, 2012 at 10:13 AM

K i looked into the code:

      public DirectionsStatusCode GetDirections(out GDirections direction, PointLatLng start, PointLatLng end, bool avoidHighways, bool avoidTolls, bool walkingMode, bool transitMode, bool sensor, bool metric)
      {
         return GetDirectionsUrl(MakeDirectionsUrl(start, end, LanguageStr, avoidHighways, avoidTolls, walkingMode, transitMode, sensor, metric), out direction);
      }

      public DirectionsStatusCode GetDirections(out GDirections direction, string start, string end, bool avoidHighways, bool avoidTolls, bool walkingMode, bool transitMode, bool sensor, bool metric)
      {
         return GetDirectionsUrl(MakeDirectionsUrl(start, end, LanguageStr, avoidHighways, avoidTolls, walkingMode, transitMode, sensor, metric), out direction);
      }

      /// <summary>
      /// NotImplemented
      /// </summary>
      /// <param name="status"></param>
      /// <param name="start"></param>
      /// <param name="end"></param>
      /// <param name="avoidHighways"></param>
      /// <param name="avoidTolls"></param>
      /// <param name="walkingMode"></param>
      /// <param name="sensor"></param>
      /// <param name="metric"></param>
      /// <returns></returns>
      public IEnumerable<GDirections> GetDirections(out DirectionsStatusCode status, string start, string end, bool avoidHighways, bool avoidTolls, bool walkingMode, bool transitMode, bool sensor, bool metric)
      {
         // TODO: add alternative directions

         throw new NotImplementedException();
      }

      /// <summary>
      /// NotImplemented
      /// </summary>
      /// <param name="status"></param>
      /// <param name="start"></param>
      /// <param name="end"></param>
      /// <param name="avoidHighways"></param>
      /// <param name="avoidTolls"></param>
      /// <param name="walkingMode"></param>
      /// <param name="sensor"></param>
      /// <param name="metric"></param>
      /// <returns></returns>
      public IEnumerable<GDirections> GetDirections(out DirectionsStatusCode status, PointLatLng start, PointLatLng end, bool avoidHighways, bool avoidTolls, bool walkingMode, bool transitMode, bool sensor, bool metric)
      {
         // TODO: add alternative directions

         throw new NotImplementedException();
      }




 string MakeDirectionsUrl(PointLatLng start, PointLatLng end, string language, bool avoidHighways, bool avoidTolls, bool walkingMode, bool transitMode, bool sensor, bool metric) { string av = (avoidHighways ? "&avoid=highways" : string.Empty) + (avoidTolls ? "&avoid=tolls" : string.Empty); // 6 string mt = "&units=" + (metric ? "metric" : "imperial"); // 7 string wk = "&mode=" + (walkingMode ? "walking" : (transitMode ? "transit" : "driving")); // 8 return string.Format(CultureInfo.InvariantCulture, DirectionUrlFormatPoint, start.Lat, start.Lng, end.Lat, end.Lng, sensor.ToString().ToLower(), language, av, mt, wk); } string MakeDirectionsUrl(string start, string end, string language, bool avoidHighways, bool walkingMode, bool transitMode, bool avoidTolls, bool sensor, bool metric) { string av = (avoidHighways ? "&avoid=highways" : string.Empty) + (avoidTolls ? "&avoid=tolls" : string.Empty); // 4 string mt = "&units=" + (metric ? "metric" : "imperial"); // 5 string wk = "&mode=" + (walkingMode ? "walking" : (transitMode ? "transit" : "driving")); // 6 return string.Format(DirectionUrlFormatStr, start.Replace(' ', '+'), end.Replace(' ', '+'), sensor.ToString().ToLower(), language, av, mt, wk); }

 

Maybe this would work?

Nov 15, 2012 at 10:49 AM
Edited Nov 15, 2012 at 10:50 AM

and with MakeRouteUrl as well:

 

(getRoute has to be changed accordingly)

static readonly string TransitStr = "&mra=ls&dirflg=r";

      string MakeRouteUrl(PointLatLng start, PointLatLng end, string language, bool avoidHighways, bool walkingMode, bool transitMode)
      {
         string opt = walkingMode ? WalkingStr : (transitMode ? TransitStr : (avoidHighways ? RouteWithoutHighwaysStr : RouteStr));
         return string.Format(CultureInfo.InvariantCulture, RouteUrlFormatPointLatLng, language, opt, start.Lat, start.Lng, end.Lat, end.Lng, Server);
      }

      string MakeRouteUrl(string start, string end, string language, bool avoidHighways, bool walkingMode, bool transitMode)
      {
         string opt = walkingMode ? WalkingStr : (transitMode ? TransitStr : (avoidHighways ? RouteWithoutHighwaysStr : RouteStr));
         return string.Format(RouteUrlFormatStr, language, opt, start.Replace(' ', '+'), end.Replace(' ', '+'), Server);
      }
Nov 15, 2012 at 2:23 PM

That code is is:GoogleMapProvider.cs

 

Can I change it myself? Is there sourceCode of the HotBuilt so I can change the .cs and compile a new dll for me?

Sry I am amateur :/

Coordinator
Nov 15, 2012 at 7:07 PM

browse to source tab and download/pull the code, i'l test it later and add it if it works

Nov 16, 2012 at 9:06 AM
Edited Nov 16, 2012 at 10:24 AM

mh...

I can't get the source code to work. I download it and opened the .sln in Visual Studio2012 and I get a lot of errors when trying to rebuild / compile.

Edit: got it :)

Nov 16, 2012 at 11:14 AM

I don't get it to work :/

It gives me a new route with getRoute and it also draws the line. But it isn't the same as in google. It is a little bit different (sometimes not following the streets and go from public transport station A to B without following the road / rails.)

The directions don't work at all. It still gives me driving directions even though I added the correct "&mode=transit"

 

I don't know what I am missing

Coordinator
Nov 16, 2012 at 11:37 AM

check their api, maybe they change something again...

Nov 16, 2012 at 11:44 AM
Edited Nov 16, 2012 at 11:47 AM

Ok I checked their api, you have to add a departureTime as well when asking for directions.

http://maps.googleapis.com/maps/api/directions/json?origin=Uzhhorod&destination=Zhytomyr&sensor=false&departure_time=1353069348&mode=transit

This one works fine.

But when trying in my code:

 

      string MakeDirectionsUrl(string start, string end, string language, bool avoidHighways, bool walkingMode, bool transitMode, bool avoidTolls, bool sensor, bool metric)
      {
          string av = (avoidHighways ? "&avoid=highways" : string.Empty) + (avoidTolls ? "&avoid=tolls" : string.Empty); // 4
          string mt = "&units=" + (metric ? "metric" : "imperial");     // 5
          string wk = "&mode=" + (walkingMode ? "walking" : (transitMode ? "transit&departure_time=1353069348" : "driving")); // 6

          return string.Format(DirectionUrlFormatStr, start.Replace(' ', '+'), end.Replace(' ', '+'), sensor.ToString().ToLower(), language, av, mt, wk);
      }

 

 

I still only get Driving directions back when asking for the steps of GDirections s; I don't even know why I get driving directions back at all xD Normaly it should say "invalid request"....

Nov 16, 2012 at 11:50 AM
Edited Nov 16, 2012 at 12:52 PM

I am an idiot :D

Works now.... I set the wrong parameter -.-"

 

edit1:

Now only the problem with the route remains not being the exact same route as in googemaps.com.

I had a look at GetRoutePoints to see why it is missing some points.... but it seems to be complicated... not as easy as direction and directionSteps

 

edit2:

In this example:

http://maps.googleapis.com/maps/api/directions/xml?origin=Alexanderplatz&destination=Brandenburgertor%20Berlin&sensor=false&departure_time=1353069348&mode=transit

step has another undercategory called "line" and you would need the "short_name" from that line also be able to get from steps. So in code you could say: steps.shortname or even steps.line.shortname

 

edit3:

ok I think routing doesnt work because the xml has "transit_details" (undercategory of step) which contains points. And they are not added to the route.

Nov 16, 2012 at 1:47 PM
Edited Nov 16, 2012 at 2:09 PM

I give up on the get Route.

But do you have any tipps on how to get the "short_name" in <transit_details><line><short_name> in such a xml file in your gmap.net?

http://maps.googleapis.com/maps/api/directions/xml?origin=Alexanderplatz&destination=Brandenburgertor%20Berlin&sensor=false&departure_time=1353069348&mode=transit

Coordinator
Nov 16, 2012 at 2:06 PM

actualy route is encoded:

<points>
eaq_IgqzpA{@zAEJWd@V~A
</points>

..there is function to decode it already, need some coding

Nov 16, 2012 at 2:28 PM
Edited Nov 16, 2012 at 2:35 PM

thx!

I got everything working with the directions.

 

But I don't get the route. Don't you use the API for the route as well? Or do you send a normal http request? Look like normal http request.

I don't know what the ouput file looks like when driving and when using transit.

 

EDIT: I looked into the RouteCache and opened a file. There is no difference in the file when using transit or driving hence everything should be working as intended and google just doesn't give the same points as in googlemaps. In googlemaps it follows the roads/rails of the transit and in gmap.net it doesn't. These informations are just missing (maybe a problem with copyrights)

Nov 16, 2012 at 3:10 PM

Thank you for your help! I got everything to work.

I created a new GMap.NET.Core.dll and used it in my project.

Only Problem: When debugging it loads very long showing these messages in debug:

GMapProviders.GoogleMap.Version: m@196000000, old: m@182000000, consider updating source
GMapProviders.GoogleHybridMap.Version: h@196000000, old: h@182000000, consider updating source
GMapProviders.GoogleSatelliteMap.Version: 121, old: 115, consider updating source
GMapProviders.GoogleTerrainMap.Version: t@129,r@196000000, old: t@129,r@182000000, consider updating source

 

Do you know what the problem is? And how to fix it?

Coordinator
Nov 16, 2012 at 4:22 PM

it's not a problem, use release build

Nov 18, 2012 at 11:58 AM

But the hot build works better in general :/

Only when I change the code it takes longer to load.

Coordinator
Nov 18, 2012 at 12:13 PM

?

Nov 19, 2012 at 8:29 AM

The Hot Built starts faster when I debug my program.

The release built takes much longer.

Coordinator
Nov 19, 2012 at 10:03 AM

hot build is release build, if you build it your self, speed is the same ;}

Nov 19, 2012 at 10:37 AM
Edited Nov 19, 2012 at 10:39 AM

O.o

But every time it checks like this:

GMapProviders.GoogleMap.Version: m@196000000, old: m@182000000, consider updating source

It takes another 10s to load.

 

Release build is 2011 and Hot build is 2012 >_>

Coordinator
Nov 19, 2012 at 11:00 AM

i mean release/debug build, it has nothing to do with the files shared on the codeplex

Nov 19, 2012 at 2:11 PM

thank you. Worked like a charm :)