Skip to content

Commit

Permalink
Region support
Browse files Browse the repository at this point in the history
  • Loading branch information
xissburg committed Apr 9, 2012
1 parent a4a8236 commit 1b48d3d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
24 changes: 21 additions & 3 deletions SVGeocoder/SVGeocoder.m
Expand Up @@ -310,9 +310,24 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection {

NSDictionary *addressDict = [placemarkDict valueForKey:@"address_components"];
NSDictionary *coordinateDict = [[placemarkDict valueForKey:@"geometry"] valueForKey:@"location"];
NSDictionary *boundsDict = [[placemarkDict valueForKey:@"geometry"] valueForKey:@"bounds"];

float lat = [[coordinateDict valueForKey:@"lat"] floatValue];
float lng = [[coordinateDict valueForKey:@"lng"] floatValue];
CLLocationDegrees lat = [[coordinateDict valueForKey:@"lat"] floatValue];
CLLocationDegrees lng = [[coordinateDict valueForKey:@"lng"] floatValue];
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(lat, lng);

// Set the region radius as half the diagonal of the bounds
NSDictionary *northEastDict = [boundsDict objectForKey:@"northeast"];
NSDictionary *southWestDict = [boundsDict objectForKey:@"southwest"];
CLLocationDegrees northEastLatitude = [[northEastDict objectForKey:@"lat"] floatValue];
CLLocationDegrees northEastLongitude = [[northEastDict objectForKey:@"lng"] floatValue];
CLLocation *northEastLocation = [[CLLocation alloc] initWithLatitude:northEastLatitude longitude:northEastLongitude];
CLLocationDegrees southWestLatitude = [[southWestDict objectForKey:@"lat"] floatValue];
CLLocationDegrees southWestLongitude = [[southWestDict objectForKey:@"lng"] floatValue];
CLLocation *southWestLocation = [[CLLocation alloc] initWithLatitude:southWestLatitude longitude:southWestLongitude];
CLLocationDistance radius = [northEastLocation distanceFromLocation:southWestLocation]/2;
[northEastLocation release];
[southWestLocation release];

NSMutableDictionary *formattedAddressDict = [[NSMutableDictionary alloc] init];
NSMutableArray *streetAddressComponents = [NSMutableArray arrayWithCapacity:2];
Expand Down Expand Up @@ -347,8 +362,11 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
if([streetAddressComponents count] > 0)
[formattedAddressDict setValue:[streetAddressComponents componentsJoinedByString:@" "] forKey:(NSString*)kABPersonAddressStreetKey];

SVPlacemark *placemark = [[SVPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(lat, lng) addressDictionary:formattedAddressDict];
CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:coordinate radius:radius identifier:[placemarkDict objectForKey:@"formatted_address"]];

SVPlacemark *placemark = [[SVPlacemark alloc] initWithRegion:region addressDictionary:formattedAddressDict];
[formattedAddressDict release];
[region release];

placemark.formattedAddress = [placemarkDict objectForKey:@"formatted_address"];

Expand Down
3 changes: 3 additions & 0 deletions SVGeocoder/SVPlacemark.h
Expand Up @@ -16,6 +16,9 @@
}

@property (nonatomic, readwrite) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) CLRegion *region;
@property (nonatomic, retain) NSString * formattedAddress;

- (id)initWithRegion:(CLRegion *)region addressDictionary:(NSDictionary *)addressDictionary;

@end
16 changes: 16 additions & 0 deletions SVGeocoder/SVPlacemark.m
Expand Up @@ -14,6 +14,7 @@
@implementation SVPlacemark

@synthesize coordinate;
@synthesize region = _region;
@synthesize formattedAddress;

- (void)dealloc {
Expand All @@ -29,6 +30,21 @@ - (id)initWithCoordinate:(CLLocationCoordinate2D)aCoordinate addressDictionary:(
return self;
}

- (id)initWithRegion:(CLRegion *)region addressDictionary:(NSDictionary *)addressDictionary {

if ((self = [super initWithCoordinate:region.center addressDictionary:addressDictionary])) {
self.coordinate = region.center;
self.region = region;
}

return self;
}

- (CLRegion *)region
{
return _region;
}

- (NSString*)description {

NSDictionary *coordDict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:self.coordinate.latitude], @"latitude", [NSNumber numberWithFloat:self.coordinate.longitude], @"longitude", nil];
Expand Down

0 comments on commit 1b48d3d

Please sign in to comment.