代码之家  ›  专栏  ›  技术社区  ›  Hugo

谷歌地图-距离矩阵API-结果顺序

  •  1
  • Hugo  · 技术社区  · 8 年前

    关于谷歌地图-距离矩阵API。结果会是保证在同一顺序有要求吗?

    我需要将我的业务对象映射到API调用的结果,但我找不到方法 硬地图

    谢谢你的意见。

    雨果

    2 回复  |  直到 8 年前
        1
  •  4
  •   user3530857    8 年前

    1) 如果是JSON响应,结果将是相同的格式。

    {
        "destination_addresses": [
          "Karnataka, India"
        ],
        "origin_addresses": [
           "Delhi, India"
        ],
        "rows": [
           {
               "elements": [
                   {
                       "distance": {
                           "text": "1,942 km",
                           "value": 1941907
                       },
                       "duration": {
                           "text": "1 day 9 hours",
                           "value": 120420
                       },
                       "status": "OK"
                   }
               ]
           }
       ],
       "status": "OK"
    }
    

    b、 多重响应:

    {
        "destination_addresses": [
        "67-89 Pacific St, Brooklyn, NY 11201, USA",
        "67-89 Pacific St, Brooklyn, NY 11201, USA",
        "67-89 Pacific St, Brooklyn, NY 11201, USA",
        "67-89 Pacific St, Brooklyn, NY 11201, USA",
        "67-89 Pacific St, Brooklyn, NY 11201, USA",
        "67-89 Pacific St, Brooklyn, NY 11201, USA",
        "557-599 Dr Wesley McDonald Ave, Brooklyn, NY 11203, USA",
        "66-0-66-26 103rd St, Rego Park, NY 11374, USA",
        "1000 N Village Ave, Rockville Centre, NY 11570, USA",
        "300-448 Beach 19th St, Far Rockaway, NY 11691, USA",
        "557-599 Dr Wesley McDonald Ave, Brooklyn, NY 11203, USA",
        "66-0-66-26 103rd St, Rego Park, NY 11374, USA",
        "1000 N Village Ave, Rockville Centre, NY 11570, USA",
        "300-448 Beach 19th St, Far Rockaway, NY 11691, USA"
    ],
    "origin_addresses": [
        "566 Vermont St, Brooklyn, NY 11207, USA"
    ],
    "rows": [
        {
            "elements": [
                {
                    "distance": {
                        "text": "6.5 mi",
                        "value": 10423
                    },
                    "duration": {
                        "text": "35 mins",
                        "value": 2096
                    },
                    "status": "OK"
                },
                {
                    "distance": {
                        "text": "6.5 mi",
                        "value": 10423
                    },
                    "duration": {
                        "text": "35 mins",
                        "value": 2096
                    },
                    "status": "OK"
                },
                {
                    "distance": {
                        "text": "6.5 mi",
                        "value": 10423
                    },
                    "duration": {
                        "text": "35 mins",
                        "value": 2096
                    },
                    "status": "OK"
                },
                {
                    "distance": {
                        "text": "6.5 mi",
                        "value": 10423
                    },
                    "duration": {
                        "text": "35 mins",
                        "value": 2096
                    },
                    "status": "OK"
                },
                {
                    "distance": {
                        "text": "6.5 mi",
                        "value": 10423
                    },
                    "duration": {
                        "text": "35 mins",
                        "value": 2096
                    },
                    "status": "OK"
                },
                {
                    "distance": {
                        "text": "6.5 mi",
                        "value": 10423
                    },
                    "duration": {
                        "text": "35 mins",
                        "value": 2096
                    },
                    "status": "OK"
                },
                {
                    "distance": {
                        "text": "2.9 mi",
                        "value": 4662
                    },
                    "duration": {
                        "text": "18 mins",
                        "value": 1086
                    },
                    "status": "OK"
                },
                {
                    "distance": {
                        "text": "8.5 mi",
                        "value": 13738
                    },
                    "duration": {
                        "text": "23 mins",
                        "value": 1367
                    },
                    "status": "OK"
                },
                {
                    "distance": {
                        "text": "15.9 mi",
                        "value": 25544
                    },
                    "duration": {
                        "text": "29 mins",
                        "value": 1755
                    },
                    "status": "OK"
                },
                {
                    "distance": {
                        "text": "13.2 mi",
                        "value": 21296
                    },
                    "duration": {
                        "text": "34 mins",
                        "value": 2058
                    },
                    "status": "OK"
                },
                {
                    "distance": {
                        "text": "2.9 mi",
                        "value": 4662
                    },
                    "duration": {
                        "text": "18 mins",
                        "value": 1086
                    },
                    "status": "OK"
                },
                {
                    "distance": {
                        "text": "8.5 mi",
                        "value": 13738
                    },
                    "duration": {
                        "text": "23 mins",
                        "value": 1367
                    },
                    "status": "OK"
                },
                {
                    "distance": {
                        "text": "15.9 mi",
                        "value": 25544
                    },
                    "duration": {
                        "text": "29 mins",
                        "value": 1755
                    },
                    "status": "OK"
                },
                {
                    "distance": {
                        "text": "13.2 mi",
                        "value": 21296
                    },
                    "duration": {
                        "text": "34 mins",
                        "value": 2058
                    },
                    "status": "OK"
                }
            ]
        }
    ],
    "status": "OK"
    }
    

    2) 我认为上面的例子也应该考虑第二个问题。

    3) 与。NET对象。结果JSON可以轻松映射。下面是映射的类

    public class Response
        {
            public string Status { get; set; }
    
            [JsonProperty(PropertyName = "destination_addresses")]
            public string[] DestinationAddresses { get; set; }
    
            [JsonProperty(PropertyName = "origin_addresses")]
            public string[] OriginAddresses { get; set; }
    
            public Row[] Rows { get; set; }
    
            public class Data
            {
                public int Value { get; set; }
                public string Text { get; set; }
            }
    
            public class Element
            {
                public string Status { get; set; }
                public Data Duration { get; set; }
                public Data Distance { get; set; }
            }
    
            public class Row
            {
                public Element[] Elements { get; set; }
            }
        }
    

        2
  •  2
  •   xomena    8 年前

    距离矩阵API web服务将结果返回为 rows 每个行项目对应于一个原点的数组 origins 参数,并且顺序与原始请求中相同。每行项目包含 elements 每个元素对应一个目标的数组 destinations

    您可以查看官方文档,其中解释了距离矩阵API响应结构:

    https://developers.google.com/maps/documentation/distance-matrix/intro#DistanceMatrixResponses

    希望这有帮助!