1

我正在使用以下代码来获取输入的邮政编码的地址,它很好地遍历地址组件,但它从来没有返回街道地址,我是否遗漏了一些东西:

public class GeoCoder
{
    public class GeoLocation
    {
        public decimal Lat { get; set; }
        public decimal Lng { get; set; }
    }
    public class GeoGeometry
    {
        public GeoLocation Location { get; set; }
    } 
    public class GeoResult
    {
        public GeoGeometry Geometry { get; set; }
        public GeoAddressComponent[] Address_Components { get; set; }
    }
    public class GeoResponse
    {
        public string Status { get; set; }
        public GeoResult[] Results { get; set; }
    }
    public class GeoAddressComponent
    {
        public string Long_Name { get; set; }
        public string Short_Name { get; set; }
        public string[] Types { get; set; }
    }

    public void FindAddress(string postcode)
    {            
        string url = "http://maps.googleapis.com/maps/api/geocode/" +
            "json?address=" + postcode + "&sensor=false";

        WebResponse response = null;
        try
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create (url);
            request.Method = "GET";
            response = request.GetResponse ();
            if (response != null)
            {
                string str = null;
                using (Stream stream = response.GetResponseStream())
                {
                    using (StreamReader streamReader = new StreamReader(stream))
                    {
                        str = streamReader.ReadToEnd ();
                    }
                }

                GeoResponse geoResponse = JsonConvert.DeserializeObject<GeoResponse>(str);
                if (geoResponse.Status == "OK")
                {
                    int count = geoResponse.Results.Length;
                    if (count > 0)
                    {
                        for (int i = 0; i < count; i++)
                        {
                            int comps = geoResponse.Results[i].Address_Components.Length;
                            for (int z = 0; z < comps; z++)
                            {
                                Console.WriteLine("Test: {0}", geoResponse.Results[i].Address_Components[z].Long_Name);
                            }

                            Console.WriteLine("Lat: {0}", geoResponse.Results[i].Geometry.Location.Lat);
                            Console.WriteLine("Lng: {0}", geoResponse.Results[i].Geometry.Location.Lng);
                        }
                    }
                } 
                else
                {
                    Console.WriteLine ("JSON response failed, status is '{0}'", geoResponse.Status);
                }
            }
        } 
        catch (Exception ex)
        {

        } 
        finally
        {
            if (response != null)
            {
                response.Close ();
                response = null;
            }
        }
    }
}
4

0 回答 0