MKPolygon和点包含

时间:2015-10-23 18:09:32

标签: ios polygon kml mkpolygon

我有一个 KML 文件,其中包含所有国家/地区的所有多边形坐标。 某些国家/地区的标记超过1 <coordinates>。 我在这里要完成的是获取坐标并找到哪个多边形。我在this帖子上有了一个想法,但我的问题是我的算法错了,并返回我的坐标位于多个多边形上。 这是我的代码:

-(void)parseCoordinates:(NSString*)str{

    coordinatesArray = [str componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    NSInteger coordinatesCount = [coordinatesArray count];

    CLLocationCoordinate2D *cord2d = malloc(sizeof(CLLocationCoordinate2D) * coordinatesCount);

    for(int i=0; i<coordinatesCount;i++){

        NSString *st = [coordinatesArray objectAtIndex:i];

        if ([st isEqualToString:@""]) {
            continue;

        }

        NSArray *strArray = [st componentsSeparatedByString:@","];

        if ([strArray count]>1) {

            cord2d[i] = CLLocationCoordinate2DMake([[strArray objectAtIndex:1] doubleValue], [[strArray objectAtIndex:0] doubleValue]);

        }

        strArray=nil;
}

    free(cord2d);

    coordinatesArray = nil;

    MKPolygon* poly2 = [MKPolygon polygonWithCoordinates:cord2d count:coordinatesCount];

    CLLocationCoordinate2D mapCoordinate = CLLocationCoordinate2DMake(39.1274378,-100.9945566);

    MKMapPoint mapPoint = MKMapPointForCoordinate(mapCoordinate);

    MKPolygonRenderer *polygonView = [[MKPolygonRenderer alloc]initWithPolygon:poly2];

    CGPoint polygonViewPoint = [polygonView pointForMapPoint:mapPoint];

    BOOL mapCoordinateIsInPolygon =
    CGPathContainsPoint(polygonView.path, NULL, polygonViewPoint, NO);

    NSLog(@"Country: %@", country);
    NSLog(@"is it inside? %d",mapCoordinateIsInPolygon);

}

0 个答案:

没有答案