Ios-location-handling

提供:Dev Guides
移動先:案内検索

iOS-ロケーション処理

ユーザーがアプリケーションでコアロケーションフレームワークの助けを借りて情報にアクセスできる場合、iOSでユーザーの現在位置を簡単に見つけることができます。

ロケーション処理-関係する手順

  • ステップ1 *-シンプルなビューベースのアプリケーションを作成します。
  • ステップ2 *-プロジェクトファイルを選択し、ターゲットを選択して、次に示すようにCoreLocation.frameworkを追加します-

iOSチュートリアル

ステップ3 *- *ViewController.xib に2つのラベルを追加し、ラベルにそれぞれ latitudeLabel および longitudeLabel という名前を付けたibOutletsを作成します。

  • ステップ4 *-[ファイル]→[新規]→[ファイル…​]を選択して、新しいファイルを作成します →[Objective C class *]を選択して、[次へ]をクリックします。

ステップ5 *-NSObjectとして *"sub class of" を持つ LocationHandler としてクラスに名前を付けます。

  • ステップ6 *-作成を選択します。

ステップ7 *-次のように *LocationHandler.h を更新します-

#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

ステップ8 *- *LocationHandler.m を次のように更新します-

#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

ステップ9 *- LocationHandlerデリゲート*を実装し、2つのibOutletsを作成した場合、次のように ViewController.h を更新します-

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

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

ステップ10 *- *ViewController.m を次のように更新します-

#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

出力

アプリケーションを実行すると、次の出力が得られます-

iOSチュートリアル