Best way to use CoreLocation in multiple views

I have two views in my application, one of them is a general view in which CoreLocation does not work, calculating the location of the user while the user does other things in the view. The second view is available to the user when they touch the button, allowing them to more accurately determine the location using mapview and MapKit, I would like this location to show the location that CoreLocation already identified in the first AND view, so that the location can continue to be displayed Based on updates from CoreLocation in a different view.

Is there a better way to create a singleton that encapsulates CoreLocation material and does it refer to a view using a map or using notifications? or use another best practice for my scenario?

thank

+3
source share
5 answers

I have several applications that are used CoreLocationin several places. From what I read, you definitely want only one instance CLLocationManager. It worked great for me as a solo.

Hope this helps!

+6
source

If I were you, I would do it as follows:

  • , . , , CalculatingView , MapView / .

  • CLLocationManager CalculateView. location . CalculatingView , .

  • CLLocationManager CalculateView, self, CLLocationManager.

  • CLLocationManager, CalculateView

  • , MapView, CalculateView. , , MapView. , , , nil, ToSelector.

  • CLLocationManager, CalculateView , MapView.

  • , , MapView , , CalculateView, CLLocationManager

  • , MapView, , MapView

, , , ( ) ( MapView).

, CLLocationManager , , 3 ~ 4 , , ,

, . , , , . , .
+4

, , (, ) . , . , .

Apple LocateMe . , , .

+1

, , CLLocationManager App Delegate, .

, , CLLocationManager

....imports....

@implementation YourViewController

- (void)viewDidLoad {
   self.myLocationManager = [[UIApplication sharedApplication] delegate].yourLocationManagerVarName;
}

@end

, .

0

, , MVC. . CLLocation .

@interface User {}
@property (nonatomic, retain) CLLocation *location;
@end

@implementation User
@synthesize location;
- (void)dealloc {
  self.location = nil;
  [super dealloc];
}
@end

. .

. CLLocationManager . , GPS, , , LBS.

- . .

. . /.

viewDidLoad .

- (void)viewDidLoad {
   [self observeValueForKeyPath:@"location" ofObject:self.user change:0 context:NULL];
}

, , .

:

  • CLLocationManager . , api , , . viewDidLoad locationManager.location API.
  • Do not share materials with the application delegate. This will prevent code reuse. What if you reuse your views and the application delegate does not have a location manager?

if you need more code please ask.

0
source

Source: https://habr.com/ru/post/1785667/


All Articles