Google API 在指定的纬度和多头位置抛出异常?



下面是我的示例代码

public static string GetGeoLoc(string latitude, string longitude,
        out string Address_ShortCountryName,
        out string Address_country,
        out string Address_administrative_area_level_1,
        out string Address_administrative_area_level_1_short_name,
        out string Address_administrative_area_level_2,
        out string Address_administrative_area_level_3,
        out string Address_colloquial_area,
        out string Address_locality,
        out string Address_sublocality,
        out string Address_neighborhood)
    {
        Address_ShortCountryName = "";
        Address_country = "";
        Address_administrative_area_level_1 = "";
        Address_administrative_area_level_1_short_name = "";
        Address_administrative_area_level_2 = "";
        Address_administrative_area_level_3 = "";
        Address_colloquial_area = "";
        Address_locality = "";
        Address_sublocality = "";
        Address_neighborhood = "";
        XmlDocument doc = new XmlDocument();
        try
        {
            doc.Load("http://maps.googleapis.com/maps/api/geocode/xml?latlng=" + latitude + "," + longitude + "&sensor=false");
            XmlNode element = doc.SelectSingleNode("//GeocodeResponse/status");
            if (element.InnerText == "ZERO_RESULTS")
            {
                return ("No data available for the specified location");
            }
            else
            {
                element = doc.SelectSingleNode("//GeocodeResponse/result/formatted_address");
                string longname = "";
                string shortname = "";
                string typename = "";

                XmlNodeList xnList = doc.SelectNodes("//GeocodeResponse/result/address_component");
                foreach (XmlNode xn in xnList)
                {
                    try
                    {
                        longname = xn["long_name"].InnerText;
                        shortname = xn["short_name"].InnerText;
                        typename = xn["type"].InnerText;
                        switch (typename)
                        {
                            case "country":
                                {
                                    Address_country = longname;
                                    Address_ShortCountryName = shortname;
                                    break;
                                }
                            case "locality":
                                {
                                    Address_locality = longname;
                                    break;
                                }
                            case "sublocality":
                                {
                                    Address_sublocality = longname;
                                    break;
                                }
                            case "neighborhood":
                                {
                                    Address_neighborhood = longname;
                                    break;
                                }
                            case "colloquial_area":
                                {
                                    Address_colloquial_area = longname;
                                    break;
                                }
                            case "administrative_area_level_1":
                                {
                                    Address_administrative_area_level_1 = longname;
                                    Address_administrative_area_level_1_short_name = shortname;
                                    break;
                                }
                            case "administrative_area_level_2":
                                {
                                    Address_administrative_area_level_2 = longname;
                                    break;
                                }
                            case "administrative_area_level_3":
                                {
                                    Address_administrative_area_level_3 = longname;
                                    break;
                                }
                            default:
                                break;
                        }
                    }
                    catch (Exception e)
                    {
                        clsExHandler.Instance.Write(e);
                    }
                }
                return (element.InnerText);
            }
        }
        catch (Exception ex)
        {
            return ("(Address lookup failed: ) " + ex.Message);
        }
    }

尝试通过纬度为33.4965&经度为-112.205

我在行中得到一个无效对象的异常对象引用

**typename = xn["type"].InnerText;**

当我一步一步地调试时,没有像["类型"]这样的属性

还有其他一些语言特征为什么?

我该如何解决这个问题。

我不熟悉c#,也不确定您的代码是否正确(例如,类型不是属性,而是elementNode)。

假设您的代码是正确的,并且您可以使用node['nameOfChildNode']来选择节点,当您检查XML文件时:http://maps.googleapis.com/maps/api/geocode/xml?latlng=33.4965,-112.205&sensor=false,您将看到有带2个<type>的address_components,也有不带任何<type>的address_coompnents。

我猜您的代码不是在丢失的<type>处中断的,而是在尝试访问丢失的<type>的属性(InnerText)时中断的。

您可以做的是:使用selectSingleNode选择<type>,当它返回null时,执行回退或离开进一步处理。

http://maps.googleapis.com/maps/api/geocode/json?latlng=33.4965%20,%20-112.205&sensor=false

返回

{
   "results" : [],
   "status" : "ZERO_RESULTS"
}

因此

XmlNode element = doc.SelectSingleNode("//GeocodeResponse/status");
            if (element.InnerText == "ZERO_RESULTS")
            {
                return ("No data available for the specified location");
            }

没有捕获CCD_ 10。

我对C#不熟悉,所以我帮不上什么忙。

相关内容

  • 没有找到相关文章

最新更新