1313// You should have received a copy of the GNU General Public License
1414// along with OpenTreeMap. If not, see <http://www.gnu.org/licenses/>.
1515
16+ #import " CLLocation+AgeInSecondsLessThan.h"
1617#import " OTMInstanceSelectTableViewController.h"
1718#import " OTMPreferences.h"
1819#import " OTMAllInstancesTableViewController.h"
@@ -70,10 +71,6 @@ - (void)viewWillAppear:(BOOL)animated
7071 [[self logInOutButton ] setTitle: @" Login to OpenTreeMap" forState: UIControlStateNormal];
7172 }
7273
73- if (![self locationManager ]) {
74- [self setLocationManager: [[OTMLocationManager alloc ] initWithDistanceRestriction: NO ]];
75- }
76-
7774 [self loadInstances ];
7875}
7976
@@ -89,15 +86,15 @@ - (void)loadInstances
8986 if ([self oneOrMorePersonalInstancesInDictionary: json]) {
9087 [self updateInstancesFromDictionary: json];
9188 } else {
92- [self loadNearbyInstances ];
89+ [self startLoadingNearbyInstances ];
9390 }
9491 } else {
9592 NSLog (@" Error getting instances for %@ : %@ " , [[SharedAppDelegate loginManager ] loggedInUser ], error);
96- [self loadNearbyInstances ];
93+ [self startLoadingNearbyInstances ];
9794 }
9895 }];
9996 } else {
100- [self loadNearbyInstances ];
97+ [self startLoadingNearbyInstances ];
10198 }
10299}
103100
@@ -110,33 +107,59 @@ - (BOOL)oneOrMorePersonalInstancesInDictionary:(NSDictionary *)json
110107 return NO ;
111108}
112109
113- - (void )loadNearbyInstances
110+
111+ - (void )startUpdatingLocation
114112{
115- [ self .activityIndicatorView startAnimating ] ;
116- [[self locationManager ] findLocationWithAccuracy: kCLLocationAccuracyThreeKilometers
117- callback: ^(CLLocation *location, NSError *error) {
118- [ self .activityIndicatorView stopAnimating ];
119- // TODO: if there is an error, we need to show some list of instances
120- if (!error) {
121- [self .activityIndicatorView startAnimating ];
122- [[[OTMEnvironment sharedEnvironment ] api ]
123- getInstancesNearLatitude: location.coordinate.latitude
124- longitude: location.coordinate.longitude
125- user: nil
126- maxResults: 5
127- distance: 320000 callback: ^( id json, NSError *error) {
128- [ self .activityIndicatorView stopAnimating ];
129- if (!error) {
130- NSLog ( @" Retrieved instances: %@ " , json);
131- [ self updateInstancesFromDictionary: json];
132- } else {
133- NSLog ( @" Error getting nearby instances: %@ " , error);
134- }
135- } ];
136- } else {
137- NSLog ( @" Error finding location: %@ " , error) ;
113+ NSLog ( @" Starting location updates for the instance table view. " ) ;
114+ [[SharedAppDelegate locationManager ] setDelegate: self ];
115+ [[SharedAppDelegate locationManager ] setDesiredAccuracy: kCLLocationAccuracyThreeKilometers ];
116+ [[SharedAppDelegate locationManager ] setDistanceFilter: kCLDistanceFilterNone ];
117+ [[SharedAppDelegate locationManager ] startUpdatingLocation ];
118+ NSTimeInterval timeout = [[[OTMEnvironment sharedEnvironment ] locationSearchTimeoutInSeconds ] doubleValue ];
119+ [self performSelector: @selector ( stopUpdatingLocationAfterTimeout ) withObject: nil afterDelay: timeout ];
120+ }
121+
122+ - ( void ) stopUpdatingLocationAfterTimeout
123+ {
124+ NSLog ( @" Stopping location updates for the instance table view after timeout. " );
125+ [[SharedAppDelegate locationManager ] stopUpdatingLocation ];
126+ [[SharedAppDelegate locationManager ] setDelegate: nil ];
127+ [ self .activityIndicatorView stopAnimating ];
128+ }
129+
130+ - ( void ) startLoadingNearbyInstances
131+ {
132+ if ([CLLocationManager locationServicesEnabled ]) {
133+ [ self .activityIndicatorView startAnimating ];
134+ if ([[SharedAppDelegate locationManager ] respondsToSelector: @selector ( requestWhenInUseAuthorization )]) {
135+ [[SharedAppDelegate locationManager ] requestWhenInUseAuthorization ] ;
138136 }
139- }];
137+ [self startUpdatingLocation ];
138+ }
139+ }
140+
141+ - (void )finishLoadingNearbyInstancesWithLocation : (CLLocation*)location
142+ {
143+ [NSObject cancelPreviousPerformRequestsWithTarget: self selector: @selector (stopUpdatingLocationAfterTimeout ) object: nil ];
144+ NSLog (@" Stopping location updates for the instance table view" );
145+ [[SharedAppDelegate locationManager ] stopUpdatingLocation ];
146+ [[SharedAppDelegate locationManager ] setDelegate: nil ];
147+
148+ [[[OTMEnvironment sharedEnvironment ] api ]
149+ getInstancesNearLatitude: location.coordinate.latitude
150+ longitude: location.coordinate.longitude
151+ user: nil
152+ maxResults: 5
153+ distance: 320000 callback: ^(id json, NSError *error) {
154+ [self .activityIndicatorView stopAnimating ];
155+ if (!error) {
156+ NSLog (@" Retrieved instances: %@ " , json);
157+ [self updateInstancesFromDictionary: json];
158+ } else {
159+ // TODO: if there is an error, we need to show some list of instances
160+ NSLog (@" Error getting nearby instances: %@ " , error);
161+ }
162+ }];
140163}
141164
142165- (void )updateInstancesFromDictionary : (NSDictionary *)instances
@@ -395,5 +418,19 @@ - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
395418 }
396419}
397420
421+ #pragma mark - CLLocationManagerDelegate methods
422+
423+ - (void )locationManager : (CLLocationManager *)manager didUpdateLocations : (NSArray <CLLocation *> *)locations
424+ {
425+ CLLocation* location = locations.lastObject ;
426+ // CoreLocation caches the last location value and passes to the app when the app first asks for the user's
427+ // location. This could be a stale location from far in the past, so we filter out location results that
428+ // are too old.
429+ if ([location ageInSecondsLessThan: kOTMMaxLocationAgeInSeconds ]) {
430+ [self finishLoadingNearbyInstancesWithLocation: location];
431+ } else {
432+ NSLog (@" Skipping location %f seconds or more in age." , kOTMMaxLocationAgeInSeconds );
433+ }
434+ }
398435
399436@end
0 commit comments