Kjuly
Kjuly

Reputation: 35131

How can I match the place with the result of CLGeocoder?

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

Upvotes: 0

Views: 835

Answers (1)

marcos1490
marcos1490

Reputation: 362

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

Upvotes: 1

Related Questions