Search code examples
iosobjective-ccocoa-touchgeolocationzipcode

Get current zip code, and show closest results from NSArray


I have a NSArray of differrent zip codes. I want to get the current users location's zip code, then see if the NSArray has that zip code, and show only the objects that have the same zip code.

So far I can do all that by just getting the user's location, put it in a string, then add a for loop, and add the matching objects to another array.

The problem starts with, what if the NSArray does not have the zip code of the user's location. Is there a way to check the closest results?


Solution

  • it should be reasonably easy to find the results which are closest numerically, I think you ought be looking at NSPredicate and an enumerateUsingBlock: type method, should be more efficient than any parser you can come up with in as for loop. You could be dealing with the numerals one at a time..

    that is let us say your zip codes have five digits, you use a predicate to return a new array with only those codes with the same first number, tens of thousands. then you repeat, so you have a (significantly smaller) array with the same first two digits, thousands... etc..then at the end if you don't have the exact code well the last array will at least have the numerically neighbouring ones. but this of course is only sorting numerically, Im in australia and I don't know about your region, but here they don't always line up so perfectly, they must be laid out onto a grid map which has two dimensions after all (so either they are laid out north-south or east-west, but either way it is likely that neighbouring zip codes will not always be numerically closest). Good luck