我正在使用Apple的kmlviewer示例,但它需要来自本地指针的kml文件。我想要做的是使用url远程调用该文件。
这是Apple的原始代码:
NSString *path = [[NSBundle mainBundle] pathForResource:@"KML_Sample" ofType:@"kml"];
NSURL *url = [NSURL fileURLWithPath:path];
kmlParser = [[KMLParser alloc] initWithURL:url];
[kmlParser parseKML];
如何调用网址中的远程文件:http://www.domain.com/route.kml?
最好的问候。
答案 0 :(得分:2)
我在KMLParser中添加了一个初始化程序以使用NSData:
在KMLParser.h文件中,添加:
-(id) initWithData: (NSData *) data;
在KMLParser.m文件中添加:
-(id) initWithData: (NSData *)data
{
if (self = [super init]) {
_styles = [[NSMutableDictionary alloc] init];
_placemarks = [[NSMutableArray alloc] init];
_xmlParser = [[NSXMLParser alloc] initWithData:data];
[_xmlParser setDelegate:self];
}
return self;
}
答案 1 :(得分:0)
我会下载文件并将其保存在文档或临时目录中。基本步骤是:
请参阅Apple的这个非常有用的示例项目,其中显示如何按照上面概述的步骤下载文件并将其保存在本地: http://developer.apple.com/library/ios/#samplecode/URLCache/Introduction/Intro.html
Apple的文档中还有一个很好的分步指南: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html
然后,一旦您在本地获得该文件,使用KMLParser打开文件将无关紧要,无论您将文件保存到何处,都可以轻松打开。
答案 2 :(得分:0)
MapViewController.h :
.....
NSMutableData *webData;
KMLParser *kml;
.....
@property (nonatomic, retain) NSMutableData *webData;
MapViewController.m :
- (IBAction)showKmlData:(id)sender //Let's say you want to download kml data with a button tap, you create a method for that (you may not act like this, but the content is the same):
{
NSURL *path = [NSURL URLWithString:@"http://www.domain.com/route.kml"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:path];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection release];
[request release];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
self.webData =[NSMutableData data];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *fileName = [[[NSURL URLWithString:kmlStr] path] lastPathComponent];
//NSString *fileName = @"route.kml";
NSArray *pathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *folder = [pathArr objectAtIndex:0];
NSString *filePath = [folder stringByAppendingPathComponent:fileName];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
NSError *writeError = nil;
[webData writeToURL: fileURL options:0 error:&writeError];
if( writeError) {
NSLog(@" Error in writing file %@' : \n %@ ", filePath , writeError );
return;
}
kml = [[KMLParser parseKMLAtPath:filePath] retain];
NSArray *annotations = [kml points];
if ([[mapview overlays] count] == 0) {
[mapview addAnnotations:annotations];
NSArray *overlays = [kml overlays];
[mapview addOverlays:overlays];
MKMapRect flyTo = MKMapRectNull;
for (id <MKOverlay> overlay in overlays) {
if (MKMapRectIsNull(flyTo)) {
flyTo = [overlay boundingMapRect];
} else {
flyTo = MKMapRectUnion(flyTo, [overlay boundingMapRect]);
}
}
for (id <MKAnnotation> annotation in annotations) {
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
if (MKMapRectIsNull(flyTo)) {
flyTo = pointRect;
} else {
flyTo = MKMapRectUnion(flyTo, pointRect);
}
}
mapview.visibleMapRect = flyTo;
}
[mapview addAnnotations:annotations];
NSArray *overlays = [kml overlays];
[mapview addOverlays:overlays];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"An error has occured." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
答案 3 :(得分:0)
没有NSURLRequest
和NSURLConnection
,它很简单:
NSURL *url = [NSURL URLWithString:@"http://www.domain.com/route.kml"];
kmlParser = [[KMLParser parseKMLAsURL:url] retain];
NSArray *overlays = [kmlParser overlays];
[mapView addOverlays:overlays];
等。用于注释。
对于parseKMLAtURL
,它在Apple SDK的KMLViewer的另一个版本(1.1)中实现为:
+ (KMLParser)parseKMLAtURL:(NSURL *)url
{
NSXMLParser *xml = [[NSXMLParser alloc] initWithContentsOfURL:url];
KMLParser *parser = [[KMLParser alloc] init];
[xmlParser setDelegate:parser];
[xml parse];
[parser _assignStyles];
return [parser autorelease];
}