ios – 在Apple Maps中绘制GeoJson作为叠加层

有人能告诉我如何在Apple地图中绘制GeoJson文件作为叠加层?我想要一个完整的例子,我有多边形或多边形的形状?

如果您为我提供了使用MKMapView渲染它的代码或库,那么这个文件Countries GeoJSON就足够了

解决方法

我找到了一个解决方案,万一有人也想要帮助.
@interface HHLViewController

+ (NSArray *)countriesOverlays;

@end

@implementation HHLViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSArray *overlays = [HHLViewController countriesOverlays];
    [self.stateMapView addOverlays:overlays];
}

+ (NSArray *)countriesOverlays {
    Nsstring *fileName = [[NSBundle mainBundle] pathForResource:@"countries" ofType:@"json"];
    NSData *overlayData = [NSData dataWithContentsOfFile:fileName];

    NSArray *countries = [[NSJSONSerialization JSONObjectWithData:overlayData options:NSJSONReadingallowFragments error:nil] objectForKey:@"features"];

    NSMutableArray *overlays = [NSMutableArray array];

    for (NSDictionary *country in countries) {

        NSDictionary *geometry = country[@"geometry"];
        if ([geometry[@"type"] isEqualToString:@"polygon"]) {
            MKpolygon *polygon = [HHLViewController overlaysFrompolygons:geometry[@"coordinates"] id:country[@"properties"][@"name"]];
            if (polygon) {
                [overlays addobject:polygon];
            }


        } else if ([geometry[@"type"] isEqualToString:@"Multipolygon"]){
            for (NSArray *polygonData in geometry[@"coordinates"]) {
                MKpolygon *polygon = [HHLViewController overlaysFrompolygons:polygonData id:country[@"properties"][@"name"]];
                if (polygon) {
                    [overlays addobject:polygon];
                }
            }
        } else {
            NSLog(@"Unsupported type: %@",geometry[@"type"]);
        }
    }

    return overlays;

}

+ (MKpolygon *)overlaysFrompolygons:(NSArray *)polygons id:(Nsstring *)title
{
    NSMutableArray *interiorpolygons = [NSMutableArray arrayWithCapacity:[polygons count] - 1];
    for (int i = 1; i < [polygons count]; i++) {
        [interiorpolygons addobject:[HHLViewController polygonFromPoints:polygons[i] interiorpolygons:nil]];
    }

    MKpolygon *overlaypolygon = [HHLViewController polygonFromPoints:polygons[0] interiorpolygons:interiorpolygons];
    overlaypolygon.title = title;


    return overlaypolygon;
}

+ (MKpolygon *)polygonFromPoints:(NSArray *)points interiorpolygons:(NSArray *)polygons
{
    NSInteger numberOfCoordinates = [points count];
    CLLocationCoordinate2D *polygonPoints = malloc(numberOfCoordinates * sizeof(CLLocationCoordinate2D));

    NSInteger index = 0;
    for (NSArray *pointArray in points) {
        polygonPoints[index] = CLLocationCoordinate2DMake([pointArray[1] floatValue],[pointArray[0] floatValue]);
        index++;
    }

    MKpolygon *polygon;

    if (polygons) {
        polygon = [MKpolygon polygonWithCoordinates:polygonPoints count:numberOfCoordinates interiorpolygons:polygons];
    } else {
        polygon = [MKpolygon polygonWithCoordinates:polygonPoints count:numberOfCoordinates];
    }
    free(polygonPoints);

    return polygon;
}
@end

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...