Google API会在指定的lat&多头?

时间:2013-06-25 06:53:08

标签: c#-4.0 google-maps-api-3 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;**

当我逐步调试时,没有像[“type”]

这样的属性

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

我怎么能解决这个问题。

2 个答案:

答案 0 :(得分:0)

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

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

我猜您的代码不会丢失<type>,当您尝试访问缺少的<type>的属性(InnerText)时,它会中断。

您可以做什么:使用selectSingleNode选择<type>,当它返回时null实施后备或保留进一步处理。

答案 1 :(得分: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#,所以我无法进一步帮助。