Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8968165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:24:32+00:00 2026-06-15T17:24:32+00:00

I want to calculate the distance between points in C# with the google maps

  • 0

I want to calculate the distance between points in C# with the google maps distance matrix API.

I use the following code to make the request :

private void MapsAPICall()
    {
        //Pass request to google api with orgin and destination details
        HttpWebRequest request =
            (HttpWebRequest)WebRequest.Create("http://maps.googleapis.com/maps/api/distancematrix/json?origins="
            + "51.123959,3.326682" + "&destinations=" + "51.158089,4.145267" 
            + "&mode=Car&language=us-en&sensor=false");

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        using (var streamReader = new StreamReader(response.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();

            if (!string.IsNullOrEmpty(result))
            {
                Distance t = JsonConvert.DeserializeObject<Distance>(result);
            }
        }
    }

And then I want to parse the json answer into the Distance class:

public struct Distance
{
   // Here I want to parse the distance and duration
}

Here is an example of the json response I receive :
http://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC&destinations=San+Francisco&mode=bicycling&language=fr-FR&sensor=false

How do I parse the distance and duration into the Distance class?
This is the first time I use Json so I’m not experienced with it.

Ps: I have the json.net library installed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:24:33+00:00Added an answer on June 15, 2026 at 5:24 pm

    Web Essentials has an option to ‘Paste JSON as classes’. This will generate the classes you need to deserialize your JSON.

    Using this option will generate the following for you:

    public class Distance
    {
        public string text { get; set; }
        public int value { get; set; }
    }
    
    public class Duration
    {
        public string text { get; set; }
        public int value { get; set; }
    }
    
    public class Element
    {
        public Distance distance { get; set; }
        public Duration duration { get; set; }
        public string status { get; set; }
    }
    
    public class Row
    {
        public Element[] elements { get; set; }
    }
    
    public class Parent
    {
        public string[] destination_addresses { get; set; }
        public string[] origin_addresses { get; set; }
        public Row[] rows { get; set; }
        public string status { get; set; }
    }
    

    (If you want, you can refactor the generated code to make it more readable. You will need to add Json.NET attributes to make sure that serialization is still working)

    Then inside your code you can use the following line to deserialize your code:

    Parent result = JsonConvert.DeserializeObject<Parent>(json);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to calculate the distance between 2 geo points, for now between my
I want to calculate distance between two points. I googled it but all I
i'm trying to use the GeoKit plugin to calculate the distance between 2 points.
I need to calculate the distance between two points, but not in the regular
In an iPhone App how can you calculate distance between two points in MKMapView
I want to calculate the direct distance between to positions in metric form. (ie:
I want to make a application that calculates the distance between the user's current
i want to calculate how many pixels there are between 2 points on my
I have a Map object created through the google maps api and I want
I want to calculate the relative bearing between two geo-coordinate points. I've gone ahead

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.