简体   繁体   English

在ios中实现了doUpdateLocations委托CLLocationManager的方法

[英]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. 我在DashboardController.m中实现了-(void)localnotification 。现在我想访问localnotification并将didUpdateLocations委托方法实现到settingsController.m类中。 My approach is so far: 我的方法到目前为止:

DashBoardViewController.h 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 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: 现在在SettingController.m我这样访问:

@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. 但[locations lastObject]在settingsController类中返回null。 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 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 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 现在在DashBoardViewController和SettingsViewController中

@property (strong, nonatomic) CLLocation *currentLocation; @property(强,非原子)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. 希望它能为你效劳。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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