0

I can get the result (like locality, ISOcountryCode, etc) by CLGeocoder's reverseGeocodeLocation:completionHandler: method successfully.
But how can I match the place with the result?

e.g.: If the city (locality) of result is Hangzhou City, I can match it simply by using

if ([placemark.locality isEqualToString:@"Hangzhou City"]) {...}

But as you know, there're millions of cities, it's impossible to get the city name one by one and hard code into my app.

So, is there any way to solve this problem? Or does there any framework exist? Or just several files contain countries & cities' name that match the CLGeocoder's result? Even fuzzy coordinate matching solution is okay (I mean, a city has its own region, and I can determine the city just by coordinate, but I still need to get every city's region area at the moment).


Deployment Target iOS5.0

icodebuster
  • 8,890
  • 7
  • 62
  • 65
Kjuly
  • 34,476
  • 22
  • 104
  • 118
  • whats the data you want to get? Normally, we use the google maps APIs for this... – Blitz May 03 '12 at 23:23
  • @LordT yes, I tried GMaps API before, same problem: the data is customize, I cannot get it. So I need to get the city, then add custom data to every city...The user'll get the corresponding data when s/he arrives a city. – Kjuly May 03 '12 at 23:28
  • Can you explain a bit more about what your goal is? Are you trying to iterate over all the cities? Are you trying to store the number of times a user visits a city? What is inside the `if (...){here}`. – Patrick May 21 '12 at 19:52
  • @Patrick just want to show some interesting places to the user when s/he arrived a different city. e.g. in 'Hangzhou City', there're 3 apples around this region, and in 'California'(not sure the name), there're 5 oranges around this region. – Kjuly May 22 '12 at 00:30
  • I suggest trying geocoding the city the user is in to get a `CLPlacemark` object. Then iterate over the `areasOfInterest`. Although I have no idea how the areas of interest are determined, it is at least worth a shot. [CLPlacemark](http://developer.apple.com/library/ios/#documentation/CoreLocation/Reference/CLPlacemark_class/Reference/Reference.html) – Patrick May 22 '12 at 17:06
  • Thanks @Patrick, but I need to determined the cities & interesting places myself. My solution (not perfect) now is just do it manually... – Kjuly May 22 '12 at 23:09
  • Oh, so you have a list of interesting places somewhere in your code. You then want to geocode them to get the `locality`, and group them based on their locality? If that is what you want to do, then just store them all in an `NSDictionary` with the keys being the `locality` and the values being the `CLPlacemark`s returned by the geocoder. – Patrick May 23 '12 at 04:06
  • @Patrick Yep, that's what I want. However, how to match the `locality`? As you know, the CLPlacemark object will have different values of locality depend on the coordinate. That's why I use `if ([placemark.locality isEqualToString:@"Hangzhou City"]) {...}`.. – Kjuly May 23 '12 at 05:43

1 Answers1

1

Well there is a easier way, you can use the reverse GeocodeLocation to get the information of the place. You have to know this won't work in every city thought. For more information check Apple's CLGeocoder Class Reference and Geocoding Location Data documentation.

So you can create and object that handle the service

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>

@interface locationUtility : NSObject<CLLocationManagerDelegate>{
  CLLocationManager *locationManager;
  CLPlacemark *myPlacemark;
  CLGeocoder * geoCoder;
}

@property (nonatomic,retain) CLLocationManager *locationManager;

@end

and the implementation

#import "locationUtility.h"

@implementation locationUtility
@synthesize locationManager;

#pragma mark - Init
-(id)init {
  NSLog(@"locationUtility - init");
  self=[super init];

  locationManager = [[CLLocationManager alloc] init];
  locationManager.delegate = self;
  locationManager.desiredAccuracy = kCLLocationAccuracyBest;
  locationManager.distanceFilter = kCLDistanceFilterNone;
  [locationManager startMonitoringSignificantLocationChanges];
  geoCoder= [[CLGeocoder alloc] init];
  return self;
}

- (void) locationManager:(CLLocationManager *) manager didUpdateToLocation:(CLLocation *) newLocation
            fromLocation:(CLLocation *) oldLocation {
  [geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
     CLPlacemark *placemark = [placemarks objectAtIndex:0];
     myPlacemark=placemark; 
     // Here you get the information you need  
     // placemark.country;
     // placemark.administrativeArea;
     // placemark.subAdministrativeArea;
     // placemark.postalCode];
    }];
}

-(void) locationManager:(CLLocationManager *) manager didFailWithError:(NSError *) error {
  NSLog(@"locationManager didFailWithError: %@", error.description);
}

@end
Kjuly
  • 34,476
  • 22
  • 104
  • 118
marcos1490
  • 362
  • 3
  • 12
  • It is what I do now, just as I described in question _"it's impossible to get the city name one by one and hard code into my app."_ I need to get the city to define `myPlacemark`... Thanks for your answer anyway. :) – Kjuly May 03 '12 at 23:38