Using LocationManager from another class than Main
i'm new here, i'm new in iphone development, i'm new in Objective-c and i'm new in engligh speaking, so don't hate me :P
I'm creating an application that will use the location manager to display the distance between the own position and the positions of a list of shops, like AroundME.
Now, i give the l开发者_如何学编程ocationManager property to my main class and in the .m of that class, precisely in the viewDidLoad method, i make: [locationManager startUpdatingLocation].
It all works fine, but the problem is: i need to know my position in other classes than the main, but i don't think that is a good idea making [locationManager startUpdatingLocation] every time i need it, is not time overhead?
The view of my main class is always below the others, so can i retrieve the updated location in my other classes? In what way?
Creating another instance of my main class is not useful, i think, but the locationManager, once started, doesn't stop until my main class is released, isn't true? So i think that it is accessible in some way.
Thanks for your attention and sorry for my awful english : )
I'm going to assume that you set your main class as the CLLocationManagerDelegate
in this case. Your delegate will receive locationManager:didUpdateToLocation:fromLocation:
messages when the location is updated and in that method you can use NSNotificationCenter
to post messages which instances of other classes can add themselves as observers for. See the documentation for NSNotificationCenter
for more info on adding observers and posting notifications.
To get the initial location in other classes besides main, you'll probably have to have a reference to the main class and retrieve it using the location
property of CLLocationManager
because your observers will only receive notifications when the location has changed.
精彩评论