iOS - Standortverwaltung

Wir können den aktuellen Standort des Benutzers in iOS leicht finden, vorausgesetzt, der Benutzer ermöglicht der Anwendung den Zugriff auf die Informationen mithilfe des zentralen Standort-Frameworks.

Standortbehandlung - Beteiligte Schritte

Step 1 - Erstellen Sie eine einfache View-basierte Anwendung.

Step 2 - Wählen Sie Ihre Projektdatei aus, wählen Sie dann Ziele aus und fügen Sie CoreLocation.framework wie unten gezeigt hinzu. -

Step 3 - Fügen Sie zwei Etiketten hinzu ViewController.xib und erstellen Sie ibOutlets mit den Bezeichnungen latitudeLabel und longitudeLabel beziehungsweise.

Step 4 - Erstellen Sie eine neue Datei, indem Sie Datei → Neu → Datei ... → auswählen Objective C class und klicken Sie auf Weiter.

Step 5 - Benennen Sie die Klasse als LocationHandler mit "sub class of" als NSObject.

Step 6 - Wählen Sie erstellen.

Step 7 - Update LocationHandler.h wie folgt -

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

@protocol LocationHandlerDelegate <NSObject>

@required
-(void) didUpdateToLocation:(CLLocation*)newLocation 
   fromLocation:(CLLocation*)oldLocation;
@end

@interface LocationHandler : NSObject<CLLocationManagerDelegate> {
   CLLocationManager *locationManager;
}
@property(nonatomic,strong) id<LocationHandlerDelegate> delegate;

+(id)getSharedInstance;
-(void)startUpdating;
-(void) stopUpdating;

@end

Step 8 - Update LocationHandler.m wie folgt -

#import "LocationHandler.h"
static LocationHandler *DefaultManager = nil;

@interface LocationHandler()

-(void)initiate;

@end

@implementation LocationHandler

+(id)getSharedInstance{
   if (!DefaultManager) {
      DefaultManager = [[self allocWithZone:NULL]init];
      [DefaultManager initiate];
   }
   return DefaultManager;
}

-(void)initiate {
   locationManager = [[CLLocationManager alloc]init];
   locationManager.delegate = self;
}

-(void)startUpdating{
   [locationManager startUpdatingLocation];
}

-(void) stopUpdating {
   [locationManager stopUpdatingLocation];
}

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:
   (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
   if ([self.delegate respondsToSelector:@selector
   (didUpdateToLocation:fromLocation:)]) {
      [self.delegate didUpdateToLocation:oldLocation 
      fromLocation:newLocation];
   }
}
@end

Step 9 - Update ViewController.h wie folgt, wo wir die implementiert haben LocationHandler delegate und erstelle zwei ibOutlets -

#import <UIKit/UIKit.h>
#import "LocationHandler.h"

@interface ViewController : UIViewController<LocationHandlerDelegate> {
   IBOutlet UILabel *latitudeLabel;
   IBOutlet UILabel *longitudeLabel;
}
@end

Step 10 - Update ViewController.m wie folgt -

#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
   [super viewDidLoad];
   [[LocationHandler getSharedInstance]setDelegate:self];
   [[LocationHandler getSharedInstance]startUpdating];
}

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

-(void)didUpdateToLocation:(CLLocation *)newLocation 
 fromLocation:(CLLocation *)oldLocation {
   [latitudeLabel setText:[NSString stringWithFormat:
   @"Latitude: %f",newLocation.coordinate.latitude]];
   [longitudeLabel setText:[NSString stringWithFormat:
   @"Longitude: %f",newLocation.coordinate.longitude]];
}
@end

Ausgabe

Wenn wir die Anwendung ausführen, erhalten wir die folgende Ausgabe: