Google地理编码地图响应数组

时间:2014-06-09 23:36:09

标签: c# arrays json google-maps google-maps-api-3

我正在使用Google地理编码API包装程序库(https://code.google.com/p/gmaps-api-net/)来获取/映射完整的地址,但不时地响应未正确映射,因为Google未返回所有地址类型和数组上的索引不会反映我想要的值的正确位置,也不会抛出空异常。

CODE

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

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

1 个答案:

答案 0 :(得分:0)

对于返回的类型或address_components的顺序,我们不作任何保证。

迭代所有address_components,测试types是否与特定类型匹配以及何时设置相关变量。