简体   繁体   中英

implement didUpdateLocations delegate method of CLLocationManager in ios

I have implemented -(void)localnotification in DashboardController.m.Now I want to access localnotification and implement didUpdateLocations delegate methods into settingsController.m class. My approach is so far:

DashBoardViewController.h

#import <CoreLocation/CoreLocation.h>

@class AppDelegate;
@interface DashBoardViewController : UIViewController<CLLocationManagerDelegate, UIAlertViewDelegate>{

    AppDelegate *appDel;
}
@property(nonatomic,strong) CLLocationManager *locationManager;
@property (nonatomic,retain) CLLocation *current;

-(void)localnotification;
@end

DashBoardViewController.m

#import "DashBoardViewController.h"
#import "AppDelegate.h"

@interface DashBoardViewController ()
@end 
@implementation DashBoardViewController

@synthesize current,locationManager;
- (void)viewDidLoad
{
    [super viewDidLoad];
    appDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
    [self localnotification];
}

-(void)localnotification{ 
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [locationManager startMonitoringSignificantLocationChanges];
}

Now in SettingController.m I am accessing like this:

@implementation SettingsViewController
DashBoardViewController *dash;

- (void)viewDidLoad {
    [super viewDidLoad];
    appDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
    dash=[[DashBoardViewController alloc] init];
    [dash localnotification];
    NSArray *locations;
    [self locationManager:appDel.locationManager didUpdateLocations:locations];
}


- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    //location 1
    CLLocation *locA = [[CLLocation alloc] initWithLatitude:appDel.latVal longitude:appDel.longVal];

    //location 2
    CLLocation *locB = [[CLLocation alloc] initWithLatitude:appDel.locationManager.location.coordinate.latitude longitude:appDel.locationManager.location.coordinate.longitude ];

    appDel.latVal = appDel.locationManager.location.coordinate.latitude; //Latitute of the location
    appDel.longVal = appDel.locationManager.location.coordinate.longitude; //Longitude of the location

    //Difference between location 1 & location 2
    CLLocationDistance dist = [locA distanceFromLocation:locB];
    NSLog(@"Difference from Settings: %ld",lroundf(dist));
    NSLog(@"Last Location's Object Settings: %@",[locations lastObject]);
}

But [locations lastObject] is returning null in the settingsController class. In view Controller class I don't have to define the locations array but in settings class as I only accessing localnotification method I have declare the locations array otherwise it gives error. What should I pass

[self locationManager:appDel.locationManager didUpdateLocations:?];

Here is the best way to use Location manager in multiple view controllers:

in AppDelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, strong) CLLocationManager * locationManager;
@property (nonatomic, strong) CLLocation *currentLocation;

in AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    if([CLLocationManager locationServicesEnabled]){
        currentLocation_ = [[CLLocation alloc] init];
        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;
        [locationManager startUpdatingLocation];
    }
}

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
    currentLocation_ = newLocation;
}

Now in DashBoardViewController & SettingsViewController

@property (strong, nonatomic) CLLocation *currentLocation;

- (void)viewDidLoad{
    [super viewDidLoad];
    if([CLLocationManager locationServicesEnabled]){
        AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
        currentLocation_ = [[CLLocation alloc] initWithLatitude:appDelegate.currentLocation.coordinate.latitude longitude:appDelegate.currentLocation.coordinate.longitude];
    }
}

Note: Its for an example, You can improve it as per your need.

Hope it will work for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM