-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRMGeocoder.m
More file actions
139 lines (109 loc) · 3.93 KB
/
RMGeocoder.m
File metadata and controls
139 lines (109 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//
// RMGeocoder.m
//
// Created by Robert Mogos
//
#import "RMGeocoder.h"
#import <MapKit/MKPlacemark.h>
@interface RMGeocoder (PrivateMethods)
- (void)startFetching;
@end
@implementation RMGeocoder
@synthesize delegate = delegate_;
- (void)locate{
translatePosition_ = NO;
[self startFetching];
}
- (void)locateAndRetriever{
translatePosition_ = YES;
[self startFetching];
}
- (void)startFetching{
hasBeenGeolocalized_ = NO;
if (!locationManager_) {
locationManager_ = [[CLLocationManager alloc] init];
locationManager_.distanceFilter = kCLDistanceFilterNone;
locationManager_.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager_ setDelegate:self];
}
[locationManager_ startUpdatingLocation];
[self performSelector:@selector(stopUpdatingLocation:)
withObject:@"Timed Out"
afterDelay:20.0];
}
- (void)retrieveAddressWithCoordinates:(CLLocationCoordinate2D)coords{
MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:coords];
[geocoder setDelegate:self];
if (geocoder.placemark) {
if ([delegate_ respondsToSelector:@selector(rmGeocoder:didFinishWithPlacemark:andLocation:)]) {
[delegate_ rmGeocoder:self didFinishWithPlacemark:geocoder.placemark andLocation:locationManager_.location];
}
[geocoder autorelease];
return;
}
[geocoder start];
}
#pragma mark CLLocationManager Delegate
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation{
#if !(TARGET_IPHONE_SIMULATOR)
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
if (locationAge > 5.0) return;
#endif
if (newLocation.horizontalAccuracy < 0) return;
if (!hasBeenGeolocalized_){
hasBeenGeolocalized_ = YES;
[NSObject cancelPreviousPerformRequestsWithTarget:self
selector:@selector(stopUpdatingLocation:)
object:@"Timed Out"];
if ([delegate_ respondsToSelector:@selector(rmGeocoder:didFinishWithLocation:)]) {
[delegate_ rmGeocoder:self didFinishWithLocation:newLocation];
}
if (translatePosition_) {
[self retrieveAddressWithCoordinates:newLocation.coordinate];
}
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
[locationManager_ stopUpdatingLocation];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(stopUpdatingLocation:) object:@"Timed Out"];
if ([delegate_ respondsToSelector:@selector(rmGeocoder:didFinishWithError:)]) {
[delegate_ rmGeocoder:self didFinishWithError:error];
}
}
- (void)stopUpdatingLocation:(NSString *)state{
[locationManager_ stopUpdatingLocation];
if ([delegate_ respondsToSelector:@selector(rmGeocoder:didFinishWithError:)]) {
[delegate_ rmGeocoder:self didFinishWithError:nil];
}
}
#pragma mark MKReverseGeocoderDelegate
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark{
if (placemark) {
if ([delegate_ respondsToSelector:@selector(rmGeocoder:didFinishWithPlacemark:andLocation:)]) {
[delegate_ rmGeocoder:self didFinishWithPlacemark:placemark andLocation:locationManager_.location];
}
[geocoder setDelegate:nil];
[geocoder autorelease];
[locationManager_ stopUpdatingLocation];
}else{
[geocoder cancel];
[geocoder start];
}
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error{
[locationManager_ stopUpdatingLocation];
if ([delegate_ respondsToSelector:@selector(rmGeocoder:didFinishWithError:)]) {
[delegate_ rmGeocoder:self didFinishWithError:error];
}
[geocoder setDelegate:nil];
[geocoder autorelease];
}
-(void)dealloc{
[locationManager_ setDelegate:nil];
[locationManager_ stopUpdatingLocation];
[locationManager_ release];
[super dealloc];
}
@end