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