-1

I want to know how to find the distance between two cities in c# using Google Maps Distance Matrix API

https://maps.googleapis.com/maps/api/directions/json?origin=Khouribga&destination=Casablanca

But, I don't know how to parse json

I want to display the result in a textbox

AwesomeBob2341
  • 135
  • 2
  • 14
  • 1
    So....what do you expect us to do for you? Have you even tried researching it first? Do you need us to google it for you? Is that what you're asking? – rory.ap Jan 12 '16 at 18:14
  • 3
    So your question isn't about the google maps api, but about json. Try using google and searching for something like ".net parsing json". – Necoras Jan 12 '16 at 18:14
  • 3
    So go figure out how to parse json. You obviously haven't made any effort to research this or tried anything yet. Please do so and come back when you have a specific question. – tnw Jan 12 '16 at 18:15
  • 2
    Possible duplicate of [.NET JSON parser comparison](http://stackoverflow.com/questions/2682260/net-json-parser-comparison) – Necoras Jan 12 '16 at 18:16
  • I did try, and I found the something here [Link](http://stackoverflow.com/questions/13999187/distance-between-addresses) But it didn't work for me – Just a student Jan 12 '16 at 18:19
  • 2
    What does that link have to do with JSON parsing? – Necoras Jan 12 '16 at 18:28
  • 2
    `But it didn't work for me` is not a good description of the problem. How are we supposed to help you if that's all you tell us? – tnw Jan 12 '16 at 18:30

1 Answers1

1

The JSON you're getting has everything:

routes.legs.distance.text

...
   "routes":[
      {
         "bounds":{
            "northeast":{
               "lat":33.5731351,
               "lng":-6.893281399999999
            },
            "southwest":{
               "lat":32.885973,
               "lng":-7.6432116
            }
         },
         "copyrights":"Map data ©2016 Google",
         "legs":[
            {
               "distance":{
                  "text":"126 km",   //here is the distance
                  "value":125977
               }
...

Just use a .net JSON parser and get this value.

Sample:

dynamic data = JsonConvert.DeserializeObject(json);
string distance = data.routes[0].legs.distance.text;

PS: I have not compiled this code sample, just created on assumption. If you get an exception please let me know.

Shaharyar
  • 12,254
  • 4
  • 46
  • 66