-2

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?

user4593252
  • 3,496
  • 6
  • 29
  • 55
  • 3
    It seems you have a problem with your code. However, we can't help unless we have [code or information that can reproduce the problem](http://stackoverflow.com/help/mcve). Otherwise, we are just blindly guessing. – gunr2171 Oct 31 '14 at 16:49
  • But is it really that inefficient to build a list of states and provinces? Even if there are 100,000+, it would be a very fast lookup with a Hashset – cost Oct 31 '14 at 16:50
  • Question has been edited to include code and call to the bing service – user4593252 Oct 31 '14 at 17:07
  • 2
    I voted to reopen because it is now a specific question, but you haven't told us what part of the code is giving you problems. – JasonMArcher Oct 31 '14 at 17:32
  • Thanks. I have posed a specific question in my last edit. – user4593252 Oct 31 '14 at 17:42

1 Answers1

0

Use the Bing Maps REST Location API to geocoder your addresses. Documentation can be found here: http://msdn.microsoft.com/en-us/library/ff701715.aspx

Information on how to use this service in c# can be found here:

http://msdn.microsoft.com/en-us/library/jj819168.aspx

http://msdn.microsoft.com/en-us/library/jj870778.aspx

I also recommend taking a look at these tips for this service: http://blogs.bing.com/maps/2013/02/14/bing-maps-rest-service-tips-tricks/

rbrundritt
  • 16,570
  • 2
  • 21
  • 46