我正在开发一个iOS 5应用程序。
我想开发一个GPX解析器,但我想知道是否已经有开发过了。
您知道是否有Objective-c GPX解析器吗?
我正在开发一个iOS 5应用程序。
我想开发一个GPX解析器,但我想知道是否已经有开发过了。
您知道是否有Objective-c GPX解析器吗?
目前,Objective-C 没有特定的 GPX 解析器。
不过这并不是问题,因为 GPX 只是 XML,所以您可以使用任何 XML 解析器来处理 GPX 数据。请参阅Ray Wenderlich's tutorial on iOS XML parsers 了解一些示例。
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;
}
下面提到的iOS GPX Framework,看起来很不错,所以我可能会在某个时候转投它。