2013-08-12 2 views
1

В моем приложении iOS я использовал фреймворк под названием "iOS KML Framework". Я добавил все виды KML файлы к моей карте, и теперь я создал простой KML файл с этим фрагментом кода:Загрузить KML-файл на сервер

- (void)createCustomKML { 
    KMLRoot *root = [KMLRoot new]; 

    KMLDocument *doc = [KMLDocument new]; 
    doc.name = @"Custom Route"; 
    doc.descriptionValue = @"A user created route"; 
    root.feature = doc; 

    KMLPlacemark *placemark = [KMLPlacemark new]; 
    placemark.name = @"Custom Route"; 
    placemark.descriptionValue = @"A user created route"; 
    [doc addFeature:placemark]; 


    KMLLineStyle *lineStyle = [KMLLineStyle new]; 
    [lineStyle setColor:@"FF0000FF"]; 
    [lineStyle setWidth:2.0]; 

    KMLStyle *style = [KMLStyle new]; 
    style.lineStyle = lineStyle; 
    [placemark addStyleSelector:style]; 


    KMLLineString *lineString = [KMLLineString new]; 
    lineString.tessellate = 1; 

    KMLCoordinate *coordinate = [KMLCoordinate new]; 
    [coordinate setLatitude:52.342155]; 
    [coordinate setLongitude:4.835847]; 

    KMLCoordinate *coordinate1 = [KMLCoordinate new]; 
    [coordinate1 setLatitude:52.345301]; 
    [coordinate1 setLongitude:4.823659]; 

    [lineString addCoordinate:coordinate]; 
    [lineString addCoordinate:coordinate1]; 
    placemark.geometry = lineString; 

} 

и было интересно, смогу ли я каким-то образом загрузить этот файл на сервер. Возможно ли это, и если да, то как мне начать?

ответ

0
- (void)createCustomKML:(NSString*)name :(NSMutableArray*)lattArray :(NSMutableArray*)longArray { 
    KMLRoot *root = [KMLRoot new]; 

    KMLDocument *doc = [KMLDocument new]; 
    doc.name = @"Custom Route"; 
    doc.descriptionValue = @"A user created route"; 
    root.feature = doc; 

    KMLPlacemark *placemark = [KMLPlacemark new]; 
    placemark.name = @"Custom Route"; 
    placemark.descriptionValue = @"A user created route"; 
    [doc addFeature:placemark]; 


    KMLLineStyle *lineStyle = [KMLLineStyle new]; 
    [lineStyle setColor:@"FF0000FF"]; 
    [lineStyle setWidth:2.0]; 

    KMLStyle *style = [KMLStyle new]; 
    style.lineStyle = lineStyle; 
    [placemark addStyleSelector:style]; 


    KMLLineString *lineString = [KMLLineString new]; 
    lineString.tessellate = 1; 

    for (int i=0; i<[lattArray count]; i++) { 
     KMLCoordinate *coordinate = [KMLCoordinate new]; 
     [coordinate setLatitude:[[lattArray objectAtIndex:i]floatValue]]; 
     [coordinate setLongitude:[[longArray objectAtIndex:i]floatValue]]; 
     [lineString addCoordinate:coordinate]; 
    } 

    placemark.geometry = lineString; 

    [self uploadKMLFile:root :name]; 
} 

- (void)uploadKMLFile:(KMLRoot*)data :(NSString*)name { 
    NSString *urlString =[NSString stringWithFormat:@"URL HERE"]; 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
    [request setURL:[NSURL URLWithString:urlString]]; 
    [request setHTTPMethod:@"POST"]; 

    NSString *boundary = @"_187934598797439873422234"; 
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
    [request setValue:contentType forHTTPHeaderField: @"Content-Type"]; 
    [request setValue:@"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" forHTTPHeaderField:@"Accept"]; 
    [request setValue:@"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/536.26.14 (KHTML, like Gecko) Version/6.0.1 Safari/536.26.14" forHTTPHeaderField:@"User-Agent"]; 
    [request setValue:@"http://google.com" forHTTPHeaderField:@"Origin"]; 

    NSMutableData *body = [NSMutableData data]; 
    [body appendData:[[NSString stringWithFormat:@"Content-Length %d\r\n\r\n", [data.kml length] ] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"picture\"; filename=\"%@.kml\"\r\n", name] dataUsingEncoding:NSUTF8StringEncoding]]; 

    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 

    [body appendData:[NSData dataWithData:[data.kml dataUsingEncoding:NSUTF8StringEncoding]]]; 

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 


    [request setHTTPBody:body]; 
    [request addValue:[NSString stringWithFormat:@"%d", [body length]] forHTTPHeaderField:@"Content-Length"]; 


    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 
    NSLog(@"%@", returnString); 
}