views:

70

answers:

1

The determination of a position takes some time. When and where should the location manager be started?

Now I'm starting the location update one view before the result view (which needs the location) is loaded. If the user taps to fast I get 0.0 coordinates.

To get the right timing the startUpdatingLocation should be called three views before the result view. The problem here is I would have to pass the value trough this three view controllers.

The next thing is that I need the location also in another view (not the views mentioned above). I thought I will create an own location listener class. But when will this class be instantiated and the coordinates (longitude, latitude) passed through? A protocol doesn't help me, because only the class which created my location listener will get the results back.

I read a bit and come up with the following possible solutions:

1) Use of the Notification Center (see this solution)

2) Create a property of my location manager in the AppDelegate

At 1) I would have the problem that the view, which needs the results wouldn't have be created before.

How would the implementation of 2) would look like? I create an instance of the location manager in the app delegate and then I access some ivars like latitude/longitude of the AppDelegate? If I would take the solution described here I would have to implement the CLLocationManagerDelegate every time. Wouldn't it be better to have one class for doing that?

Perhaps you have a third solution?

+1  A: 

A third solution may be to use a singleton class where you store the latitude and longitude of the location in a CLLocationCoordinate2D. You can then use the following code to access the stored coordinate from any view in your project

DataController* dataController = [DataController sharedDataController];
CLLocationCoordinate2D currentLocationCoordinate = dataController.coordinate;
Jeroen de Leeuw
The location is determined on start of app (in `AppDelegate`). I created a Singleton which instantiates `CLLocationManager`. Now only one instance of the location manager can be created, and the location data is available all the time. When? On start of app. Where? In `AppDelegate`.
testing
The AppDelegate is also a singleton class, personally I like to create a singleton class of my own to store the application data. I'm glad I could help.
Jeroen de Leeuw