检查CLLocation是否处于Shape状态?

时间:2015-01-05 16:36:31

标签: ios shape cllocation

我正在寻找一种方法来检测用户是在荷兰还是在比利时。我不想打电话给API或使用用户的电话服务,所以一切都需要在本地完成,应该是简单快捷的小工具。我每秒都有用户位置,并希望每分钟检查一次。

目标: 用户正在使用我们的应用程序旅行,我想在他/她到达比利时/荷兰时显示不同的视图。

我找到了http://www.diva-gis.org/datadown与荷兰和比利时的两个形状文件。这些文件很小,现在我正在寻找一种方法来创建这个方法:

- (BOOL)isUserInNetherlands:(CLLocation)location;

应检查位置是否在shapefile的内容中。

荷兰的shapefile看起来像这样:

netherlands shapefile

我可以将shapefile转换为sqlLite数据库,但不知道下一步该做什么。

任何想法?

2 个答案:

答案 0 :(得分:4)

一种方法是:

1)将shapefile转换为geoJson

2)在代码中加载geoJson文件数据。

3)从这些gps点创建多边形(注意:CountryDetectorController是我的控制器类)

+ (MKPolygon *)overlaysFromPolygons:(NSArray *)polygons title:(NSString *)title;
{
    NSMutableArray *interiorPolygons = [NSMutableArray arrayWithCapacity:[polygons count] - 1];
    for (int i = 1; i < [polygons count]; i++) {
        [interiorPolygons addObject:[CountryDetectorController polygonFromPoints:polygons[i] interiorPolygons:nil]];
    }

    MKPolygon *overlayPolygon = [CountryDetectorController 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;
}

4)从多边形

创建一个CGPath
+ (CGPathRef)pathForPolygon:(MKPolygon*)aPolygon;
{
    CGMutablePathRef mpr = CGPathCreateMutable();

    MKMapPoint *polygonPoints = aPolygon.points;
    size_t nCount = aPolygon.pointCount;

    for (int p = 0; p < nCount; p++)
    {
        MKMapPoint mp = polygonPoints[p];

        if (p == 0)
            CGPathMoveToPoint(mpr, NULL, mp.x, mp.y);
        else
            CGPathAddLineToPoint(mpr, NULL, mp.x, mp.y);
    }

    return mpr; //Keep in memory;
}

5)检查坐标是否在CGPath中(pathsBelgium是一个具有NSValue CGPath的数组)

- (void)checkCoordinate:(CLLocationCoordinate2D)coordinate;
{
    MKMapPoint mapPoint = MKMapPointForCoordinate(coordinate);
    CGPoint mapPointAsCGP = CGPointMake(mapPoint.x, mapPoint.y);

    BOOL userIsInBelgium = FALSE;
    for(NSValue *valuePathBE in pathsBelgium)
    {
        CGPathRef pathBe;
        [valuePathBE getValue:&pathBe];

        BOOL pointInBelgium = CGPathContainsPoint(pathBe, NULL, mapPointAsCGP, FALSE);
        if(pointInBelgium)
        {
            userIsInBelgium = TRUE;
            break;
        }
    }

    NSLog(@"User is in Belgium: %i", userIsInBelgium);
}

答案 1 :(得分:0)

更简单的方法是获取用户的位置,然后执行反向地理编码。然后,您可以从Apple的API中提取国家/地区,而不是自己完成所有工作。 更多信息:https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/UsingGeocoders/UsingGeocoders.html