Example #1
0
        }         // end of method GetGeoNamesSuggestions

        /// <summary>
        /// Retrieves data from the Here Maps Web Service
        /// </summary>
        private void GetHereSuggestions()
        {
            string        response = null;
            List <string> matches  = new List <string>();

            if (strJSON != null)
            {
                if (UtilityMethod.IsValidJson(strJSON))
                {
                    HereGeoLocation.cityGeographicalData = JsonConvert.DeserializeObject <HereGeoLocation>(strJSON);
                    List <HereGeoLocation.Response.View.Result> places = HereGeoLocation.cityGeographicalData.response.view.result;
                    int matchCount = places.Count;

                    if (matchCount == 1)
                    {
                        HereGeoLocation.Response.View.Result                  place    = places[0];
                        HereGeoLocation.Response.View.Result.Location         location = place.location;
                        HereGeoLocation.Response.View.Result.Location.Address address  = location.address;
                        HereGeoLocation.Response.View.Result.Location.Address.AdditionalData country         = address.additionalData[0];
                        HereGeoLocation.Response.View.Result.Location.DisplayPosition        displayPosition = location.displayPosition;
                        string label = place.location.address.label;

                        cityName    = label.Substring(0, label.IndexOf(","));
                        countryCode = UtilityMethod.ToProperCase(country.value);
                        countryName = (string)UtilityMethod.worldCountryCodes[countryName];
                        regionName  = null;
                        regionCode  = null;
                        Latitude    = displayPosition.latitude.ToString();
                        Longitude   = displayPosition.longitude.ToString();

                        response = label;
                        matches.Add(response);
                    }// end of if block
                    else
                    {
                        foreach (HereGeoLocation.Response.View.Result place in places)
                        {
                            StringBuilder match = new StringBuilder();

                            // We only need the cities with the same name
                            if (!place.location.address.label.Equals(PreferencesForm.searchCity.ToString(),
                                                                     StringComparison.OrdinalIgnoreCase))
                            {
                                continue;
                            }

                            string m = place.location.address.label;

                            if (!m.Contains(m.ToString()))
                            {
                                matches.Add(m.ToString());
                            } // end of if block
                        }     // end of for each loop
                    }         // end of the else block
                }             // end of if block
            }                 // end of if block
        }                     // end of method GetHereSuggestions
Example #2
0
        }// end of method AccessFormControlsOnOtherThread

        /// <summary>
        /// Retrieves data from the Geo Names Web Service
        /// </summary>
        private void GetGeoNamesSuggestions()
        {
            List <string> matches       = new List <string>();
            string        localCityName = null;
            string        response      = null;

            cityName    = null;
            countryName = null;
            countryCode = null;
            regionCode  = null;
            regionName  = null;

            if (strJSON != null)
            {
                if (UtilityMethod.IsValidJson(strJSON))
                {
                    GeoNamesGeoLocation.cityGeographicalData = JsonConvert.DeserializeObject <GeoNamesGeoLocation>(strJSON);
                    int matchCount = GeoNamesGeoLocation.cityGeographicalData.totalResultsCount;

                    if (matchCount == 1)
                    {
                        GeoNamesGeoLocation.GeoNames place = GeoNamesGeoLocation.cityGeographicalData.geonames[0];

                        cityName      = place.name;
                        countryCode   = place.countryCode;
                        countryName   = place.countryName;
                        localCityName = place.toponymName;
                        regionCode    = place.adminCode1;
                        regionName    = place.countryCode.Equals("US") ?
                                        UtilityMethod.usStatesByCode[regionCode].ToString() :
                                        null;
                        Latitude  = place.lat;
                        Longitude = place.lng;

                        if (regionName != null)
                        {
                            response = $"{cityName}, {regionName}, {countryName}";
                        }// end of if block
                        else
                        {
                            response = $"{cityName}, {countryName}";
                        } // end of else block
                    }     // end of if block
                    else
                    {
                        foreach (GeoNamesGeoLocation.GeoNames place in GeoNamesGeoLocation.cityGeographicalData.geonames)
                        {
                            StringBuilder match = new StringBuilder();

                            // We only need the cities with the same name
                            if (!place.name.Equals(PreferencesForm.searchCity.ToString(),
                                                   StringComparison.OrdinalIgnoreCase))
                            {
                                continue;
                            }

                            match.Append(place.name);

                            // Geo Names may not return adminCodes1 for some results
                            if (place.adminCodes1 != null)
                            {
                                if (place.adminCodes1.iso != null &&
                                    !UtilityMethod.IsNumeric(place.adminCodes1.iso))
                                {
                                    string region = place.adminCodes1.iso ?? null;

                                    match.Append(", " + region);
                                } // end of outer if block
                            }     // end of if block

                            match.Append(", " + place.countryName);

                            // Always verify that the adminName1 and countryName does not indicate a city already added
                            if (!matches.ToString().Contains($"{place.adminName1}, {place.countryName}"))
                            {
                                // Redundancy check
                                if (!matches.Contains(match.ToString()))
                                {
                                    matches.Add(match.ToString());
                                } // end of if block
                            }     // end of if block
                        }         // end of for each loop
                    }             // end of else block
                }                 // end of if block
            }                     // end of if block
            else
            {
                UtilityMethod.LogMessage(UtilityMethod.LogLevel.SEVERE, "The web service returned invalid JSON data",
                                         "CityDataService::GetGeoNamesSuggestions");
            }// end of else block

            // ensure that a service did not request this data
            if (frmPreferences != null)
            {
                if (matches.Count > 0)
                {
                    matchList = matches.ToArray();
                    PreferencesForm.cityNames = matchList;
                }// end of if block
                else
                {
                    matches.Clear();
                    matchList = new string[] { "No match found..." };
                    PreferencesForm.cityNames = matchList;
                }// end of else block

                // since it is known that invoke will be required, the if statement is not
                // necessary but, it is good for practice.
                if (frmPreferences.InvokeRequired)
                {
                    AccessWidget wid = new AccessWidget(AccessFormControlProperty);
                    frmPreferences.Invoke(wid, new object[] { frmPreferences });
                } // end of if block
            }     // end of if block
        }         // end of method GetGeoNamesSuggestions