带有邮政编码工作的Google地理编码API,但没有街道地址?

时间:2012-10-07 18:11:31

标签: asp.net google-geocoding-api

我使用以下代码获取输入的邮政编码的地址,它通过地址组件循环很好,但它从未返回街道地址,我错过了一些可能:

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

0 个答案:

没有答案