0

下面是我的示例代码

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;**

当我逐步调试时,没有像 ["type"] 这样的属性

还有一些其他的语言字符为什么?

我该如何解决这个问题。

4

2 回答 2

0

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

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

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

您可以做什么:用于selectSingleNode 选择<type>并在它返回时null执行回退或离开进一步处理。

于 2013-06-25T09:01:06.887 回答
0

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

是不抓ZERO_RESULTS

我不熟悉 C#,所以我无法提供进一步的帮助。

于 2013-06-25T11:26:04.257 回答