C# 使用Asp.NETMVC使用谷歌地图API计算2个Zipcode之间的距离#

C# 使用Asp.NETMVC使用谷歌地图API计算2个Zipcode之间的距离#,c#,asp.net-mvc,google-maps-api-3,jsonserializer,C#,Asp.net Mvc,Google Maps Api 3,Jsonserializer,今天我需要创建一些功能来计算zipcodes之间的距离,这对我来说并不容易。幸运的是,我找到了一个很好的方法,我相信这对其他人会有帮助。目标是根据商店和客户之间的距离收取送货费 我试过一些方法,最好的也是最简单的 首先,我需要基于google的json响应创建一个viewModel 请求返回的json结构具有以下格式: { "destination_addresses" : [ "Centro, Juiz de Fora - MG, 36013-210, Brasil" ],

今天我需要创建一些功能来计算zipcodes之间的距离,这对我来说并不容易。幸运的是,我找到了一个很好的方法,我相信这对其他人会有帮助。

目标是根据商店和客户之间的距离收取送货费

我试过一些方法,最好的也是最简单的

首先,我需要基于google的json响应创建一个viewModel

请求返回的json结构具有以下格式:

{
     "destination_addresses" : [ "Centro, Juiz de Fora - MG, 36013-210, Brasil" ],
     "origin_addresses" : [ "Passos, Juiz de Fora - MG, 36026-460, Brasil" ],
     "rows" : [
          {
            "elements" : [
                 {
                   "distance" : 
                        {
                          "text" : "3,1 km",
                          "value" : 3149
                        },
                   "duration" : 
                        {
                          "text" : "11 minutos",
                          "value" : 645
                        },
                   "status" : "OK"
                 }
             ]
         }
     ],
     "status" : "OK"
}
viewModel的属性应该与json返回属性等效

这样,我的viewModel如下所示:

//Class for "distance" and "duration" which has the "text" and "value" properties.
public class CepElementNode
{
    public string text { get; set; }

    public string value { get; set; }
}

//Class for "distance", "duration" and "status" nodes of "elements" node 
public class CepDataElement
{
    public CepElementNode distance { get; set; }

    public CepElementNode duration { get; set; }

    public string status { get; set; }
}

//Class for "elements" node
public class CepDataRow
{
    public List<CepDataElement> elements { get; set; }
}

//Class which wrap the json response
public class RequestCepViewModel
{
    public List<string> destination_addresses { get; set; }

    public List<string> origin_addresses { get; set; }

    public List<CepDataRow> rows { get; set; }

    public string status { get; set; }
}
//具有“text”和“value”属性的“distance”和“duration”类。
公共类ceElementNode
{
公共字符串文本{get;set;}
公共字符串值{get;set;}
}
//“元素”节点的“距离”、“持续时间”和“状态”节点的类
公共类数据元素
{
公共CepElementNode距离{get;set;}
公共CepElementNode持续时间{get;set;}
公共字符串状态{get;set;}
}
//“元素”节点的类
公共类数据行
{
公共列表元素{get;set;}
}
//类,该类包装json响应
公共类RequestCepViewModel
{
公共列表目标地址{get;set;}
公共列表源地址{get;set;}
公共列表行{get;set;}
公共字符串状态{get;set;}
}
注意,数组元素如“目的地地址”、“源地址”和“行”应该是C#中的一个列表。通过这种方式,我们可以在RequestCepViewModel实例中反序列化json响应

最后,在我的行动中,我有以下代码:

var url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=36026460&destinations=36013210&mode=driving&language=en-EN&sensor=false";
WebRequest webRequest = WebRequest.Create(url);
WebResponse response = webRequest.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);

var jsonResponseString = reader.ReadToEnd();

var viewModel = new JavaScriptSerializer().Deserialize<RequestCepViewModel>(jsonResponseString);
var url=”http://maps.googleapis.com/maps/api/distancematrix/json?origins=36026460&destinations=36013210&mode=driving&language=en-EN&sensor=false”;
WebRequest WebRequest=WebRequest.Create(url);
WebResponse=webRequest.GetResponse();
Stream responseStream=response.GetResponseStream();
StreamReader=新的StreamReader(responseStream);
var jsonResponseString=reader.ReadToEnd();
var viewModel=new JavaScriptSerializer()。反序列化(jsonResponseString);

希望它能帮助别人

问候