コード例 #1
0
    protected void searchButton_Click(object sender, EventArgs e)
    {
        try
        {
            //Get the latitude and longitude from the given zipcode
            Assignment5ServiceRefs.Service1Client serviceRefs = new Assignment5ServiceRefs.Service1Client();
            String   zipCode = zipCodeTB.Text;
            String[] latLong = serviceRefs.getLatLong(zipCode);
            if (latLong[0].Equals("false"))
            {
                LibrariesLabel.Text = "Invalid Zipcode";
            }
            else
            {
                //Get the place id of athe nearby libraries
                String locationIdQuery = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" + latLong[0] + "," + latLong[1] + "&type=library&rankby=distance&key=" + API_KEY;
                //Create a web client for the http get operation
                WebClient client = new WebClient();
                //Get the result in JSON format
                string        response   = client.DownloadString(locationIdQuery);
                StringBuilder addsb      = new StringBuilder();
                dynamic       jsonReader = JObject.Parse(response);
                int           length     = jsonReader.results.Count < 5 ? jsonReader.results.Count : 5;
                //If the count is zero, theer are no libraries in the vicinity
                if (length == 0)
                {
                    LibrariesLabel.Text = "No Libraries within 30 miles.";
                }
                else
                {
                    for (int i = 0; i < length; i++)
                    {
                        //Get the store id from the JSON
                        String placeId = jsonReader.results[i].place_id;
                        //Construct a query to obtain the address from the place id
                        string addressQuery = "https://maps.googleapis.com/maps/api/place/details/json?placeid=" + placeId + "&key=" + API_KEY;

                        //Get the address in JSON format
                        string  addResponse = client.DownloadString(addressQuery);
                        dynamic addJson     = JObject.Parse(addResponse);
                        String  libraryName = jsonReader.results[i].name;
                        addsb.Append(libraryName);
                        addsb.Append(",");
                        addsb.Append(addJson.result.adr_address);
                        addsb.Append("<br/><br/>");
                    }
                    LibrariesLabel.Text = addsb.ToString();
                }
            }
        }
        catch (Exception exc)
        {
            LibrariesLabel.Text = exc.Message;
        }
    }