C#如何求两个城市之间的距离

C# How to find the distance between two cities

我想知道如何使用 Google 地图距离矩阵在 c# 中查找两个城市之间的距离 API

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

但是,我不知道如何解析json

我想在文本框中显示结果

您得到的 JSON 应有尽有:

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
               }
...

只需使用 .net JSON parser 并获取此值。

样本:

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

PS:我没有编译这个代码示例,只是假设创建的。如果您遇到异常,请告诉我。