我无法从谷歌地图的 api 结果中提取单个地址组件。
以下是从此 url 收到的结果:
http://maps.googleapis.com/maps/api/geocode/json?address=jobing.com+glendale+arizona&sensor=false
这个 url 在“uri”变量中。这是我用来尝试按类型检索 address_component 的代码。考虑到我对 c# 有点陌生。
String Output = client.DownloadString(uri);
Dictionary<String, Object> RinkData = JsonConvert.DeserializeObject<Dictionary<String, Object>>(Output);
Dictionary<String, Object> RinkDataResults = (Dictionary<String, Object>) RinkData["results"];
if (RinkDataResults.ContainsKey("formatted_address"))
{
Route = GetAddressComponentByType(RinkDataResults["address_components"], "route");
}
这是我正在使用的函数“GetAddressComponentByType”
protected String GetAddressComponentByType(Object AddressComponents, String AddressType)
{
Boolean MatchFound = false;
String MatchingLongName = "";
Dictionary<String, Object> AddressComponentsDict = (Dictionary<String, Object>)AddressComponents;
foreach (KeyValuePair<String, Object> ac in AddressComponentsDict)
{
Dictionary<String, Object> acDict = (Dictionary<String, Object>) ac.Value;
ArrayList acTypes = (ArrayList) acDict["types"];
foreach (String acType in acTypes)
{
if (acType == AddressType)
{
MatchFound = true;
break;
}
}
if (MatchFound)
{
MatchingLongName = (String) acDict["long_name"];
}
}
return MatchingLongName;
}
问题是,它甚至没有用到我的组件检索功能。它炸弹将 RinkData["results"] 转换为 Dictionary 说这是一个无效的转换。
有谁看到我做错了什么?或者也许有人有一个自定义对象,我可以将谷歌地图地理编码结果读入该作品?