检查 CLLocation 是否在 Shape 中?
Check if CLLocation is in Shape?
我正在寻找一种方法来检测用户是在荷兰还是在比利时。我不想打电话给 API 或使用用户的电话服务,所以一切都需要在本地完成,而且应该是简单快捷的小支票。我每秒都有用户位置,想每分钟检查一次。
目标:
用户正在使用我们的应用程序旅行,我想在 he/she 到达 Belgium/Netherlands 时显示不同的视图。
我从 http://www.diva-gis.org/datadown 找到了荷兰和比利时的两个形状文件。这些文件非常小,现在我正在寻找创建此方法的方法:
- (BOOL)isUserInNetherlands:(CLLocation)location;
应该检查位置是否在 shapefile 的内容中。
荷兰的 shapefile 看起来像这样:
我可以将 shapefile 转换为 sqlLite 数据库,但不知道下一步该做什么。
有什么想法吗?
一种更简单的方法是获取用户的位置,然后进行反向地理编码。然后你可以从 Apple 的 API 中提取国家,而不是自己做所有的工作。
更多信息:https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/UsingGeocoders/UsingGeocoders.html
一种方法是:
1) 将 shapefile 转换为 geoJson
2) 在您的代码中加载 geoJson 文件数据。
3) 从这些 gps 点创建多边形(注意:CountryDetectorController 是我的控制器 class)
+ (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为CGPaths的数组)
- (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);
}
我正在寻找一种方法来检测用户是在荷兰还是在比利时。我不想打电话给 API 或使用用户的电话服务,所以一切都需要在本地完成,而且应该是简单快捷的小支票。我每秒都有用户位置,想每分钟检查一次。
目标: 用户正在使用我们的应用程序旅行,我想在 he/she 到达 Belgium/Netherlands 时显示不同的视图。
我从 http://www.diva-gis.org/datadown 找到了荷兰和比利时的两个形状文件。这些文件非常小,现在我正在寻找创建此方法的方法:
- (BOOL)isUserInNetherlands:(CLLocation)location;
应该检查位置是否在 shapefile 的内容中。
荷兰的 shapefile 看起来像这样:
我可以将 shapefile 转换为 sqlLite 数据库,但不知道下一步该做什么。
有什么想法吗?
一种更简单的方法是获取用户的位置,然后进行反向地理编码。然后你可以从 Apple 的 API 中提取国家,而不是自己做所有的工作。 更多信息:https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/UsingGeocoders/UsingGeocoders.html
一种方法是:
1) 将 shapefile 转换为 geoJson
2) 在您的代码中加载 geoJson 文件数据。
3) 从这些 gps 点创建多边形(注意:CountryDetectorController 是我的控制器 class)
+ (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为CGPaths的数组)
- (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);
}