Вопрос

I am new to objective-c and have been trying to work on this problem for the past 2 and a half hours. So far I was able to have some success and my app can start searching for the user's location as soon as the app is launched.

I then setup a delegate for CLLocationManager based off of this advice:

"Add the syntax to declare that this class (the ViewController.h/m) is adopting this particular protocol. By default, any other classes that create an instance of this class will also automatically adopt the protocol.

@interface ViewController : UIViewController <CLLocationManagerDelegate>

The line of code listed above shows the syntax used to show ViewController adopts the CLLocationManagerDelegate protocol. We simply add it between angled brackets <> in the @interface line in the header."

So I successfully added the above line inside my ViewController.m and ViewController.h file, and then I also followed that same tutorials advice of:

"The full method is:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 

and when implemented it tells the delegate that new location data is available. We have two arguments on this method. The first lets you know which CLLocationManager provided the update and the last provides the CLLocation information which is stored in an NSArray."

Below is all of my ViewController.h code and then next wll be my ViewController.m code:

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>


@interface ViewController : UIViewController <CLLocationManagerDelegate>

@property (nonatomic, strong) IBOutlet UILabel *gpsLabel;


-(IBAction)gpsButton;


@end

Here is my ViewController.m code:

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface ViewController () <CLLocationManagerDelegate>


@property (nonatomic, strong) CLLocationManager * gpsLM;

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;

@end

@implementation ViewController


- (void)viewDidLoad
{

[super viewDidLoad];

self.gpsLM = [[CLLocationManager alloc]init];

[self.gpsLM startUpdatingLocation];

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

-(IBAction)gpsButton{

}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {

}

@end

I am confused on where to go from here. Assuming I am even doing everything correctly so far(am I?), then how do I access the location data that has been stored in the NSArray object called locations?

Thank you for the help.

Это было полезно?

Решение

Welcome to the iOS community!

First, you'll probably find it helpful to look at the CLLocationManagerDelegate reference, under the heading for the locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations method. The important bit here is:

locations: An array of CLLocation objects containing the location data. This array always contains at least one object representing the current location. If updates were deferred or if multiple locations arrived before they could be delivered, the array may contain additional entries. The objects in the array are organized in the order in which they occurred. Therefore, the most recent location update is at the end of the array.

The important thing to get is that this method is called by the system when the user's location changes.

So, for instance, you could print out a message to your console when the location updates by changing your implementation to something like:

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    CLLocation *currentLocation = [locations lastObject];
    NSLog(@"Location is: %.5f %.5f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
}

If you instead want to do something in response to user activity, you could use manager.location, which is automatically updated every time the CLLocationManager detects a new location. (If this didn't exist, you would need to add another instance variable to your class to store the most recent location, and update that in locationManager:didUpdateLocations:.)

So, for instance, if you wanted to update your label with the current location whenever the button was pressed, you could add something like:

-(IBAction)gpsButton {
    CLLocation *currentLocation = self.gpsLM.location;
    self.gpsLabel.text = [NSString stringWithFormat:@"Location is: %.5f, %.5f"];
}

(Note: this assumes that the gpsButton action and the gpsLabel outlet are hooked up to something graphically in Interface Builder.)

If you're familiar with other programming languages, this is a push versus pull distinction. CLLocationManager provides a push model (it calls your locationManager:didUpdateLocations: method to inform you immediately of changes), and also a pull model (you can ask it for the most current location through .location at any time). Which one you use will depend on what you want to do.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top