谷歌地理code地图响应阵列 [英] Google Geocode Map Response Array
问题描述
我使用的是谷歌地理编码API包装库( https://开头code.google.com / p / GMaps实现-API网/ ),以获得/图一个完整的地址,但时不时响应不正确映射,因为谷歌并没有返回所有的地址类型和数组的索引不会体现价值的正确位置我想还是会抛出空例外。
I am using a Google Geocoding API Wrapper library (https://code.google.com/p/gmaps-api-net/) to get/map a complete address but every now and then the response doesn't map correctly because Google didn't return all the address types and the indexes on the array do not reflect the correct location of the value I want or it throws a null exception.
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;
}
}
RESPONSE(JSON)
RESPONSE (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"
}
如何/什么是确保该元素的正确方法在响应的存在?
How/What is the correct way of ensuring the element exists in the response?
推荐答案
有没有为返回类型的担保或 address_components
的顺序。
There isn't a guarantee for the returned types or the order of the address_components
.
遍历所有 address_components
,测试如果类型
匹配特定的类型,当它设置相关变量
Iterate over all address_components
, test if the types
match a particular type and when it does set the related variable.
这篇关于谷歌地理code地图响应阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!