トップ «前の日記(2012-05-05) 最新 次の日記(2012-05-08)» 編集

Cocoa練習帳

iOS/iPhone/iPad/watchOS/tvOS/MacOSX/Android プログラミング, Objective-C, Cocoa, Swiftなど

2012|01|02|03|04|05|06|07|08|09|10|11|12|
2013|01|02|03|04|05|06|07|08|09|10|11|12|
2014|01|02|03|04|05|06|07|08|09|10|11|12|
2015|01|02|03|04|05|06|07|08|09|10|11|12|
2016|01|02|03|04|05|06|07|08|09|10|11|12|
2017|01|02|03|04|05|06|07|08|09|10|11|12|
2018|01|02|03|04|05|06|07|08|09|10|11|12|
2019|01|02|03|04|05|06|07|08|09|10|11|12|
2020|01|02|03|04|05|06|07|08|09|10|11|12|
2021|01|02|03|04|05|06|07|08|09|10|11|12|
2022|01|02|03|04|05|06|07|08|09|10|11|12|
2023|01|02|03|04|05|06|07|08|09|10|11|12|
2024|01|02|03|

2012-05-06 [iOS]GPSとGPX(その3)

前回からの変更点。GPX関連のクラスのインスタンスをDocumentクラスで管理するように変更。CoreLocationフレームワークを追加して、CoreLocation/CoreLocation.h をインポートする。

ViewControllerクラスのヘッダーにCLLocationManagerDelegateをプロトコルとして設定して、CoreLocation関連のインスタンスを追加。

@interface ViewController : UIViewController >CLLocationManagerDelegate<
 
@property (strong, nonatomic) IBOutlet UILabel      *messageLabel;
@property (strong, nonatomic) IBOutlet UITextView   *gpxTextView;
@property (strong, nonatomic) Document              *document;
@property (strong, nonatomic) CLLocationManager     *locationManager;
 
- (IBAction)trackPoint:(id)sender;
- (IBAction)dump:(id)sender;
@end

track pointボタンが押下され、- (IBAction)trackPoint:(id)senderが呼ばれたら、現在の位置情報をGPXデータに軌跡として登録するコードを追加する。

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    AppDelegate	*appl = nil;
	appl = (AppDelegate *)[[UIApplication sharedApplication] delegate];
	self.document = appl.document;
    
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
}
 
- (IBAction)trackPoint:(id)sender
{
    [self.locationManager startUpdatingLocation];
}
 
- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    [self.locationManager stopUpdatingLocation];
    
    GPXTrackPoint   *trkpt = nil;
    trkpt = [self.document.gpxTrack newTrackpointWithLatitude:newLocation.coordinate.latitude
                                                    longitude:newLocation.coordinate.longitude];
    trkpt.time = newLocation.timestamp;
}

_ - (void)locationManager:(CLLocationManager *)manager

didFailWithError:(NSError *)error { [self.locationManager stopUpdatingLocation]; }

これだと、どんなGPXデータが生成されているのか分からないので、ダンプする機能を追加。

- (IBAction)dump:(id)sender
{
    self.gpxTextView.text = self.document.gpxRoot.gpx;
}

これは、実機でないと上手く試せないと思う。

WayPoints

_ ソースコード

GitHubからどうぞ。
https://github.com/murakami/workbook/tree/master/ios/WayPoints - GitHub

_ 関連情報

iOS GPX Framework
GitHub
This is a iOS framework for parsing/generating GPX files. This Framework parses the GPX from a URL or Strings and create Objective-C Instances of GPX structure.
iOSプログラミング逆引きリファレンス108 ~知りたいことがすぐわかるiPhoneプログラミングテクニック~
iPhoneアプリ開発 熟達テクニック
上記、2冊には助けられた。

トップ «前の日記(2012-05-05) 最新 次の日記(2012-05-08)» 編集