1

在我的 iOS 应用程序中,我一直在使用名为"iOS KML Framework"的框架。我已将各种 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;

}

并且想知道我是否可以以某种方式将此文件上传到服务器。这可能吗?如果可以,我应该如何开始?

4

1 回答 1

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);
}
于 2013-08-13T12:21:01.650 回答