谷歌地理代码地图响应数组

本文关键字:响应 数组 地图 代码 谷歌 | 更新日期: 2023-09-27 18:08:03

我使用谷歌地理编码API包装库(https://code.google.com/p/gmaps-api-net/)获得/映射一个完整的地址,但每隔一段时间的响应不正确映射,因为谷歌没有返回所有的地址类型和数组上的索引不反映我想要的值的正确位置,或者它抛出一个空异常。

代码
    public AddressMapper(GeocodeResponse geocodeResponse)
    {
        AddressComponents = geocodeResponse.Results[0].AddressComponents;

        if (AddressComponents != null)
        {
            StreetNumber = TryParse(AddressType.StreetNumber);
            Street = TryParse(AddressType.StreetNumber);
            Neighborhood = TryParse(AddressType.StreetNumber);
            City = TryParse(AddressType.StreetNumber);
            Province = TryParse(AddressType.StreetNumber);
            Country = TryParse(AddressType.StreetNumber);
            Political = TryParse(AddressType.StreetNumber);
            PostalCode = TryParse(AddressType.StreetNumber);
            FormattedAddress = TryParse(AddressType.StreetNumber);
            //Latitude = address.Geometry.Location.Latitude.ToString(CultureInfo.InvariantCulture);
            //Longitude = address.Geometry.Location.Longitude.ToString(CultureInfo.InvariantCulture);
        }
    }
>>> This seems to always be an empty string.
    private string TryParse(AddressType addressType)
    {
        if (AddressComponents == null) return "";
        foreach (var component in AddressComponents)
        {
            return component.Types.Contains(addressType) ? component.LongName : string.Empty;
        }
    return string.Empty;
}
}

响应(JSON)

    {
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Amphitheatre Parkway",
               "short_name" : "Amphitheatre Pkwy",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Mountain View",
               "short_name" : "Mountain View",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Santa Clara County",
               "short_name" : "Santa Clara County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "94043",
               "short_name" : "94043",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
         "geometry" : {
            "location" : {
               "lat" : 37.4219998,
               "lng" : -122.0839596
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.4233487802915,
                  "lng" : -122.0826106197085
               },
               "southwest" : {
                  "lat" : 37.4206508197085,
                  "lng" : -122.0853085802915
               }
            }
         },
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}

确保元素在响应中存在的正确方法是什么?

谷歌地理代码地图响应数组

不能保证返回的类型或address_components的顺序。

遍历所有address_components,测试types是否匹配特定类型,如果匹配,则设置相关变量