[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
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;
}
これは、実機でないと上手く試せないと思う。
ソースコード
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冊には助けられた。