0

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の URL にあるリモート ファイルを呼び出すにはどうすればよいですか?

よろしくお願いします。

4

4 に答える 4

2

NSData を使用するために KMLParser に Initializer を追加しました。

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;
}
于 2012-12-11T11:17:49.293 に答える
0

ファイルをダウンロードして、ローカルのドキュメントまたは一時ディレクトリに保存します。基本的な手順は次のとおりです。

  • 目的の URL で NSURLRequest を作成します
  • その NSURLRequest で NSURLConnection を作成します
  • NSURLConnection から受信したデータを格納する NSMutableData メンバー変数を作成します。
  • NSURLConnection デリゲートのメソッドを実装します。
    • connection:didReceiveResponse (http 200 で成功したことを確認してください)
    • connection:didReceiveData (受信したデータを NSMutableData 変数に追加します)
    • connection:didFailWithError (エラー処理)
    • connectionDidFinishLoading (すべてのデータが受信され、ローカル ファイルに書き込まれます)

Apple のこの非常に便利なサンプル プロジェクトを参照してください。ここでは、ファイルをダウンロードして、上記で概説した手順に従ってローカルに保存する方法を示しています。 はじめに.html

こちらの Apple のドキュメントにもステップバイステップのガイドがあります: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html

次に、ファイルをローカルに取得したら、ファイルを保存する場所への URL を使用して KMLParser で開くのは簡単です。

于 2012-04-16T13:38:55.367 に答える
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];
}
于 2012-04-28T19:11:58.720 に答える
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];
}
于 2012-04-18T23:03:13.797 に答える