Encode & Decode Between GeoJSON & MapKit Shapes
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
Example
GeoJSONSerialization.xcworkspace
GeoJSONSerialization
GeoJSONSerialization.podspec
LICENSE
README.md

README.md

GeoJSONSerialization

GeoJSONSerialization encodes and decodes between GeoJSON and MapKit shapes, following the API conventions of Foundation's NSJSONSerialization class.

Usage

Decoding

#import <MapKit/MapKit.h>
#import "GeoJSONSerialization.h"

NSURL *URL = [[NSBundle mainBundle] URLForResource:@"map" withExtension:@"geojson"];
NSData *data = [NSData dataWithContentsOfURL:URL];
NSDictionary *geoJSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSArray *shapes = [GeoJSONSerialization shapesFromGeoJSONFeatureCollection:geoJSON error:nil];

for (MKShape *shape in shapes) {
    if ([shape isKindOfClass:[MKPointAnnotation class]]) {
        [mapView addAnnotation:shape];
    } else if ([shape conformsToProtocol:@protocol(MKOverlay)]) {
        [mapView addOverlay:(id <MKOverlay>)shape];
    }
}

After implementing the necessary MKMapViewDelegate methods, the resulting map will look something like this.


Contact

Mattt Thompson

License

GeoJSONSerialization is available under the MIT license. See the LICENSE file for more info.