我正在开发一个 iOS 5 应用程序。
我想开发一个 GPX 解析器,但我想知道在我开始开发它之前是否已经开发了一个。
你知道是否有一个 Objective-c GPX 解析器吗?
我正在开发一个 iOS 5 应用程序。
我想开发一个 GPX 解析器,但我想知道在我开始开发它之前是否已经开发了一个。
你知道是否有一个 Objective-c GPX 解析器吗?
看看: http ://terrabrowser.googlecode.com/svn/trunk/
在那里你会发现 GPSFileParser.m 和 GPSFileParser.h 这可能会对你有所帮助。
我一直在研究gpx-api一段时间。它将读取 gpx 文件并具有可用的数据模型(在我看来)。
目前没有针对 Obejective-C 的特定 GPX 解析器。
这并不是一个真正的问题,因为 GPX 只是 XML,因此您可以使用任何 XML 解析器来处理 GPX 数据。查看Ray Wenderlich 关于 iOS XML 解析器的教程以获取一些示例。
我意识到这是一个老问题,但我刚刚开始使用这个 GPX 解析器:
https://github.com/patricks/gpx-parser-cocoa
这是从这个分叉的:
https://github.com/fousa/gpx-parser-ios
下面是我正在使用的代码。它假设你有一个 IBOutlet (self.theMapView) 连接到你的 MKMapView,你已经设置了委托并将 MapKit 框架添加到你的目标,并且你有一个有效的 gpx 文件(称为test-gpx .gpx)在您的项目中。我在 Mac 应用程序中使用它,但我认为代码也适用于 iOS。
- (void)parseGPX {
NSString *gpxFilePath = [[NSBundle mainBundle] pathForResource:@"test-gpx" ofType:@"gpx"];
NSData *fileData = [NSData dataWithContentsOfFile:gpxFilePath];
[GPXParser parse:fileData completion:^(BOOL success, GPX *gpx) {
// success indicates completion
// gpx is the parsed file
if (success) {
NSLog(@"GPX success: %@", gpx);
NSLog(@"GPX filename: %@", gpx.filename);
NSLog(@"GPX waypoints: %@", gpx.waypoints);
NSLog(@"GPX routes: %@", gpx.routes);
NSLog(@"GPX tracks: %@", gpx.tracks);
[self.theMapView removeAnnotations:self.theMapView.annotations];
for (Waypoint *thisPoint in gpx.waypoints) {
// add this waypoint to the map
MKPointAnnotation *thisRecord = [[MKPointAnnotation alloc] init];
thisRecord.coordinate = thisPoint.coordinate;
thisRecord.title = thisPoint.name;
[self.theMapView addAnnotation:thisRecord];
}
for (Track *thisTrack in gpx.tracks) {
// add this track to the map
[self.theMapView addOverlay:thisTrack.path];
}
[self.theMapView setRegion:[self.theMapView regionThatFits:gpx.region] animated:YES];
} else {
NSLog(@"GPX fail for file: %@", gpxFilePath);
}
}];
}
- (MKOverlayRenderer*)mapView:(MKMapView*)mapView rendererForOverlay:(id <MKOverlay>)overlay {
MKPolylineRenderer* lineView = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
lineView.strokeColor = [NSColor orangeColor];
lineView.lineWidth = 7;
return lineView;
}
下面@Dave Robertson 提到的iOS GPX 框架看起来不错,所以我可能会在某个时候切换到它。