Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Personally, I'd be inclined to do it as follows:</p> <ol> <li><p>First, I'd be inclined to save the C array of <code>CLLocationCoordinate2D</code> coordinates that I pass to <code>MKPolygon</code> instance method <code>polygonWithCoordinates</code> (rather than a <code>MKMapPoint</code> C array). You can adapt this code if you'd rather use <code>MKMapPoints</code>, but I prefer a format which I can examine externally and make sense of (namely the latitude and longitude values of <code>CLLocationCoordinate2D</code>). So, let's assume that you make your <code>MKPolygon</code> with a line of code like so:</p> <pre><code>MKPolygon* poly = [MKPolygon polygonWithCoordinates:coordinates count:count]; </code></pre> <p>You could, therefore save the coordinates like so:</p> <pre><code>[self writeCoordinates:coordinates count:count file:filename]; </code></pre> <p>Where <code>writeCoordinates:count:filename:</code> is defined as follows:</p> <pre><code>- (void)writeCoordinates:(CLLocationCoordinate2D *)coordinates count:(NSUInteger)count file:(NSString *)filename { NSData *data = [NSData dataWithBytes:coordinates length:count * sizeof(CLLocationCoordinate2D)]; [data writeToFile:filename atomically:NO]; } </code></pre> <p>You could then make the <code>MKPolygon</code> from the file with:</p> <pre><code>MKPolygon* poly = [self polygonWithContentsOfFile:filename]; </code></pre> <p>where <code>polygonWithContentsOfFile</code> is defined as:</p> <pre><code>- (MKPolygon *)polygonWithContentsOfFile:(NSString *)filename { NSData *data = [NSData dataWithContentsOfFile:filename]; NSUInteger count = data.length / sizeof(CLLocationCoordinate2D); CLLocationCoordinate2D *coordinates = (CLLocationCoordinate2D *)data.bytes; return [MKPolygon polygonWithCoordinates:coordinates count:count]; } </code></pre></li> <li><p>Alternatively, you could read and write the array of <code>CLLocationCoordinate2D</code> in a plist format (which renders it in a human readable format), by replacing the two above methods with:</p> <pre><code>const NSString *kLatitudeKey = @"latitude"; const NSString *kLongitudeKey = @"longitude"; - (void)writeCoordinates:(CLLocationCoordinate2D *)coordinates count:(NSUInteger)count file:(NSString *)filename { NSMutableArray *array = [NSMutableArray arrayWithCapacity:count]; for (NSUInteger i = 0; i &lt; count; i++) { CLLocationCoordinate2D coordinate = coordinates[i]; [array addObject:@{kLatitudeKey:@(coordinate.latitude), kLongitudeKey:@(coordinate.longitude)}]; } [array writeToFile:filename atomically:NO]; } - (MKPolygon *)polygonWithContentsOfFile:(NSString *)filename { NSArray *array = [NSArray arrayWithContentsOfFile:filename]; NSUInteger count = [array count]; CLLocationCoordinate2D coordinates[count]; for (NSUInteger i = 0; i &lt; count; i++) { NSDictionary *dictionary = array[i]; coordinates[i].latitude = [dictionary[kLatitudeKey] doubleValue]; coordinates[i].longitude = [dictionary[kLongitudeKey] doubleValue]; } return [MKPolygon polygonWithCoordinates:coordinates count:count]; } </code></pre></li> </ol> <p>Clearly you'd want to add error checking code to make sure that the <code>writeToFile</code> and the <code>dataWithContentsOfFile</code> (or <code>arrayWithContentsOfFile</code>) succeeded, but hopefully this gives you the idea.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload