public string[] getLatLong(string zipcode)
        {
            String[] latlongStr = new String[2];

            try
            {
                //Create proxy object of the ndfdXML class
                WeatherServiceReference.ndfdXML proxy = new WeatherServiceReference.ndfdXML();

                //Get the latitude and longitude from the zipcode from the service in XML format
                var latlongXML = proxy.LatLonListZipCode(zipcode);
                //Load the latitude and longitude XML into the XmlDocument object
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(latlongXML);
                //Split the data into decimal array and return it
                latlongStr = doc.InnerText.Split(',');
            }
            catch (Exception e)
            {
                latlongStr[0] = "false";
                latlongStr[1] = e.Message;
            }
            return(latlongStr);
        }
        public string[] getWeather5day(string zipcode)
        {
            String[] weatherData = new String[10];
            String[] maxTemp     = new String[5];
            String[] minTemp     = new String[5];

            //An arraylist of parameters to be extracted from the XML
            ArrayList requiredData = new ArrayList();

            requiredData.Add("Daily Maximum Temperature");
            requiredData.Add("Daily Minimum Temperature");

            try
            {
                //Create proxy object of the ndfdXML class
                WeatherServiceReference.ndfdXML proxy = new WeatherServiceReference.ndfdXML();

                //Get the latitude and longitude from the zipcode from the service in XML format
                var latlongXML = proxy.LatLonListZipCode(zipcode);

                //Put the latitude and longitude in the decimal array
                decimal[] latlong = new decimal[2];
                latlong = getLatLong(latlongXML);

                //Get weather data for the next 5 days in the XML format
                int index          = 0;
                var weatherDataStr = proxy.NDFDgenByDay(latlong[0], latlong[1], DateTime.Now.AddDays(1), "5", "e", "24 hourly");

                //Load the XML into XmlDocument object
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(weatherDataStr);

                //Get the parameters tag from the XML
                XmlNodeList paramNode = doc.GetElementsByTagName("parameters");

                //Navigate to the child node which contains the data
                foreach (XmlNode child in paramNode)
                {
                    XmlNodeList paramChildNode = child.ChildNodes;
                    foreach (XmlNode pchild in paramChildNode)
                    {
                        XmlNodeList dataNodeList = pchild.ChildNodes;
                        String      nameStr      = "";
                        Boolean     name         = true;
                        index = 0;
                        foreach (XmlNode dataNode in dataNodeList)
                        {
                            // the first node will be the name of the property
                            //check if the property is either minimum temperature or maximum temperature
                            //Add it accordingly to the minTemp or maxTemp array
                            if (name)
                            {
                                nameStr = dataNode.InnerText;
                                name    = false;
                            }
                            else if (requiredData.Contains(nameStr))
                            {
                                if (nameStr.Equals("Daily Maximum Temperature"))
                                {
                                    maxTemp[index] = dataNode.InnerText;
                                }
                                else
                                {
                                    minTemp[index] = dataNode.InnerText;
                                }
                                index++;
                            }
                        }
                    }
                }
                //Combine the minTemp and maxTemp array into weatherData and return it
                for (int i = 0; i < 5; i++)
                {
                    weatherData[i] = maxTemp[i];
                }
                for (int i = 0; i < 5; i++)
                {
                    weatherData[5 + i] = minTemp[i];
                }
            }
            //return error message if an exception is caught
            catch (Exception e)
            {
                weatherData[0] = "error";
                weatherData[1] = e.Message;
            }
            return(weatherData);
        }
Exemplo n.º 3
0
        public string getStoreLocation(string zipcode, string storeName)
        {
            try
            {
                //Create a web client for the http get operation
                WebClient     client  = new WebClient();
                String        address = "";
                StringBuilder addsb   = new StringBuilder();

                //ndfdXML object to retrive the latitude and longitude from the zipcode
                WeatherServiceReference.ndfdXML proxy = new WeatherServiceReference.ndfdXML();
                //Get the latitude and longitude in XML data
                var latlongXML = proxy.LatLonListZipCode(zipcode);

                //Convert the XML data into String array
                decimal[] latlong = getLatLong(latlongXML);

                //Construct the query to get the nearby stores using google places api
                string locationIdQuery = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" + latlong[0] + "," + latlong[1]
                                         + "&rankby=distance&name=" + storeName + "&key=" + API_KEY;

                //Get the result in JSON format
                string  response   = client.DownloadString(locationIdQuery);
                dynamic jsonReader = JObject.Parse(response);

                //Retrieve the first result as it is the nearest store
                String retStoreName = jsonReader.results[0].name;
                //if the result store name matches the queried store name , continue parsing the JSON
                if (retStoreName.ToLower().Contains(storeName.ToLower()))
                {
                    //Start constructing the address string
                    addsb.Append(retStoreName);
                    addsb.Append(",");

                    //Get the store id from the JSON
                    String storeId = jsonReader.results[0].place_id;
                    //Construct a query to obtain the address from the store id
                    string addressQuery = "https://maps.googleapis.com/maps/api/place/details/json?placeid=" + storeId + "&key=" + API_KEY;

                    //Get the address in JSON format
                    string  addResponse = client.DownloadString(addressQuery);
                    dynamic addJson     = JObject.Parse(addResponse);

                    int count = addJson.result.address_components.Count;
                    //If the JSON is empty then there are no stores in the vicinity
                    if (count == 0)
                    {
                        address = "No stores within 30 miles";
                    }
                    //Iterate over the JSON elements to construct the address string
                    else
                    {
                        for (int i = 0; i < count; i++)
                        {
                            addsb.Append(addJson.result.address_components[i].long_name);
                            if (i < count - 1)
                            {
                                addsb.Append(",");
                            }
                        }
                    }
                }
                else
                {
                    address = "No stores within 30 miles";
                }
                address = addsb.ToString();
                return(address);
            }
            catch
            {
                return("No stores within 30 miles");
            }
        }