I have an interesting challenge. I have a worldwide Google map search form where the user is told that they can enter a city, state or zip. Entering zip: great. Entering city: awesome. Entering city AND state: beautiful. Entering just a state: no joy.
I need to tell if a user has entered just a city or just a state/province. I have found that the Bing Geolocation service can do this. Location information comes down in model.Location
and can be city, state, city & state, or zip.
Currently the code I'm using:
int n = 0;
bool isZipCode = int.TryParse(model.Location, out n);
string[] nonZipLocation = model.Location.Split(',');
//get lat and long
XmlDocument xmlResponse = App.Geocoder.UserDefinedFunctions.Geocode(
"",
isZipCode ? "" : nonZipLocation.Length > 1 ? nonZipLocation[1] : "", //state
isZipCode ? "" : nonZipLocation[0], //city
isZipCode ? n.ToString() : "", //zip
""); //street address
XmlNodeList locations = xmlResponse.GetElementsByTagName("Location");
if (locations != null && locations.Count > 0) {
model.Longitude = locations[0]["Point"]["Longitude"].InnerText;
model.Latitude = locations[0]["Point"]["Latitude"].InnerText;
}//end if
Where I'm running into problems is: 1. I first check to see if it's a numeric zip and place it in zip if it is. 2. I split on commas to see if I have a city and state/province. (start of the problem) If there's more than one element, I place a city and state. If there's only one element on the split, it goes into city.
How can I tell if the user has entered a city or a state/province? I know one solution is to build an exhaustive list of states, territories, and provinces. I know another possible solution might me a third party service (unless bing geolocation can do it for me and I don't know it). Or is there another solution?