Google driving directions in C# - Update

| 13 Comments | No TrackBacks
I've updated my GDirections C# class to allow for easier access to the actual driving directions.  The code file can be found here.  As usual, you will need your own Google Maps API key, which you can get for free at http://code.google.com/apis/maps/signup.html.  You will also need JSON.NET to parse the JSON response from Google's service.

Example code:

GDirections directions = new GDirections(startAddress, endAddress);

if (directions.IsValid)
    Response.Write(directions.RoutesHtml[0]);
else
    Response.Write("Route not valid.");


No TrackBacks

TrackBack URL: http://www.sagecraft-studios.com/mt-tb.cgi/12

13 Comments

I get an error on the following:

_data = readJson(new JsonReader(new StringReader(json)));

Error 1 Cannot create an instance of the abstract class or interface 'Newtonsoft.Json.JsonReader' C:\Visual Studio 2008\svnprojects\Honda\HondaDealerLocator\App_Code\GDirections.cs 139 26 C:\...\HondaDealerLocator\

I don't have a working dwv computer to check this on right now, but make sure json.net version 2 is referenced. I think that was the current version when I originally wrote this. I now feel compelled to update my code. :)

Fixed it by changing the following:

_data = readJson(new JsonReader(new StringReader(json)));

to:

_data = readJson(new JsonTextReader(new StringReader(json)));

Also - any idea how I could output "Walking" directions as well as "Driving" directions?

It looks like to get walking directions you add the argument "dirflg=w" to the request URL. I haven't looked at the response to see if it gives the same structure, but it may work. Who knows! :)

Hello,

How can I get a result whenever google maps gives a choice between two places ? It is possible to get an exception and act accordingly with a dialog box ?

Many thanks anyway for this great work !

Glompix -

Appreciate the code you have written. Would like to customize this for our needs: eg. use lat, long in place of addresses as input parameters. Can you help?

Actually, this should already be possible. Just construct the object as following (example lat/lng):

GDirections directions = new GDirections("From 50.25252, -50.25252 to 51.25252, -51.25252");

can i pass lat/long values instead of address to the gDirection function.I am developing a webservice where i get start address and end address in latitude/longitude .Else is there any way to convert lat/long to address using c#?

Thanks for the code, JSON was being a real challenge to me...

Any idea how NOT to request the directions ? All I really care about is the time and distance between 2 points, not the actual direction. In Google Maps API, I would use {getstep:false} in the javascript's GDirection() creator, do you have any idea how to do the same your way via HTTP ?

Thanks again

I'm not sure actually. A good way to figure this out would be to create a page containing a script that does the GDirections {getstep: false} thing you are talking about. Open the page in Firefox, open Firebug, and check what the script is asking for from Google in the console or net panel. From here, you can just add a parameter to the GDirections constructor and reconfigure the request URL accordingly.

I'm actually intrigued myself, since I first implemented this class only to get driving times. (a directions requirement did come later, though) Let me know if you find out anything.

Thanks for the insight, Firebug says "doflg=", and the resulting response is much smaller, but still holds the distance and time information I was looking for.

Would like to add that also found this to be easier in deserializing the JSON data coming back from GMap

http://blog.sb2.fr/post/2008/11/23/Get-Address-A-to-B-Distance-using-GoogleMaps-with-C.aspx

Hey, thanks for the great share.

Is there a way to change the language of the results?

Regards,

Leandro Nuñez.

Powered by Movable Type 4.21-en

Recent Comments

  • Leandro Nuñez: Hey, thanks for the great share. Is there a way read more
  • hchang: Would like to add that also found this to be read more
  • hchang: Thanks for the insight, Firebug says "doflg=", and the resulting read more
  • glompix: I'm not sure actually. A good way to figure this read more
  • hchang: Thanks for the code, JSON was being a real challenge read more
  • Rakesh: can i pass lat/long values instead of address to the read more
  • glompix: Actually, this should already be possible. Just construct the object read more
  • KN: Glompix - Appreciate the code you have written. Would like read more
  • Jean Mourmon: Hello, How can I get a result whenever google maps read more
  • glompix: It looks like to get walking directions you add the read more