确定CLLocationCoordinate2D是否在定义的区域(边界)内? [英] Determine whether a CLLocationCoordinate2D is within a defined region (bounds)?

查看:247
本文介绍了确定CLLocationCoordinate2D是否在定义的区域(边界)内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种简单的方法来确定CLLocationCoordinate2D是否位于由一系列其他CLLocationCoordinate2D定义的任意形状的边界内。形状可能足够大,需要考虑大圆路径。



CL曾经有一个圆形区域和containsCoordinate:call测试,但在iOS7中已经弃用,dox不包含可能替代它的提示。我找不到任何其他的例子,特别是那些适用于多边形的例子。



这里有很多类似的问题,但它们与iOS没有特别关系,而且,我似乎无法找到一个通常用于大圆聚合物的方法。

解决方案

这是一个例子(使用阿冈昆省立公园一种可能适合你的方法。



为此目的使用 CGPathContainsPoint MKMapView 不是必需的。



也无需创建 MKPolygon ,甚至无需使用 CLLocationCoordinate2D MKMapPoint 结构。它们只是让代码更容易理解。



下面的截图是根据数据创建的,仅用于说明目的。



< pre class =lang -c prettyprint-override> int numberOfCoordinates = 10;

//此示例在Algonquin Provincial Park周围绘制一个带有10坐标
//的粗多边形。根据需要使用尽可能多的坐标
//以达到所需的精度。
CLLocationCoordinate2D algonquinParkCoordinates [numberOfCoordinates];
algonquinParkCoordinates [0] = CLLocationCoordinate2DMake(46.105,-79.4);
algonquinParkCoordinates [1] = CLLocationCoordinate2DMake(46.15487,-78.80759);
algonquinParkCoordinates [2] = CLLocationCoordinate2DMake(46.16629,-78.12095);
algonquinParkCoordinates [3] = CLLocationCoordinate2DMake(46.11964,-77.70896);
algonquinParkCoordinates [4] = CLLocationCoordinate2DMake(45.74140,-77.45627);
algonquinParkCoordinates [5] = CLLocationCoordinate2DMake(45.52630,-78.22532);
algonquinParkCoordinates [6] = CLLocationCoordinate2DMake(45.18662,-78.06601);
algonquinParkCoordinates [7] = CLLocationCoordinate2DMake(45.11689,-78.29123);
algonquinParkCoordinates [8] = CLLocationCoordinate2DMake(45.42230,-78.69773);
algonquinParkCoordinates [9] = CLLocationCoordinate2DMake(45.35672,-78.90647);

//从上面的坐标创建CGPath ...
CGMutablePathRef mpr = CGPathCreateMutable();

for(int p = 0; p< numberOfCoordinates; p ++)
{
CLLocationCoordinate2D c = algonquinParkCoordinates [p];

if(p == 0)
CGPathMoveToPoint(mpr,NULL,c.longitude,c.latitude);
else
CGPathAddLineToPoint(mpr,NULL,c.longitude,c.latitude);
}

//设置一些测试坐标并测试它们...
int numberOfTests = 7;
CLLocationCoordinate2D testCoordinates [numberOfTests];
testCoordinates [0] = CLLocationCoordinate2DMake(45.5,-78.5);
testCoordinates [1] = CLLocationCoordinate2DMake(45.3,-79.1);
testCoordinates [2] = CLLocationCoordinate2DMake(45.1,-77.9);
testCoordinates [3] = CLLocationCoordinate2DMake(47.3,-79.6);
testCoordinates [4] = CLLocationCoordinate2DMake(45.5,-78.7);
testCoordinates [5] = CLLocationCoordinate2DMake(46.8,-78.4);
testCoordinates [6] = CLLocationCoordinate2DMake(46.1,-78.2);

for(int t = 0; t< numberOfTests; t ++)
{
CGPoint testCGPoint = CGPointMake(testCoordinates [t] .longitude,testCoordinates [t] .latitude) ;

BOOL tcInPolygon = CGPathContainsPoint(mpr,NULL,testCGPoint,FALSE);

NSLog(@tc [%d](%f,%f)in polygon =%@,
t,
testCoordinates [t] .latitude,
testCoordinates [t] .longitude,
(tcInPolygon?@是:@否));
}

CGPathRelease(mpr);

以下是上述测试的结果:



多边形中的

  tc [0](45.500000,-78.500000)=是
tc [1](45.300000,-79.100000)in polygon = No
tc [ 2](45.100000,-77.900000)in polygon = No
tc [3](47.300000,-79.600000)in polygon = No
tc [4](45.500000,-78.700000)in polygon = Yes
tc [5](46.800000,-78.400000)in polygon = No
tc [6](46.100000,-78.200000)in polygon =是



此屏幕截图仅用于说明数据(实际 MKMapView 不需要运行上面的代码):




I am trying to find a simple method to determine whether a CLLocationCoordinate2D lies within the boundaries of an arbitrary shape defined by a series of other CLLocationCoordinate2D's. The shapes may be large enough that great-circle paths need to be considered.

CL used to have a circular region and the containsCoordinate: call to test against, but this has been deprecated in iOS7 and the dox do not contain a hint of what might replace it. I cannot find any other examples, notably one that works on polygons.

There are many similar questions here on SO, but they are not related to iOS specifically, and again, I can't seem to find one that works generally on great-circle polys.

解决方案

Here's an example (using Algonquin Provincial Park) of an approach that may work for you.

To use CGPathContainsPoint for this purpose, an MKMapView is not required.

Nor is it necessary to create an MKPolygon or even to use the CLLocationCoordinate2D or MKMapPoint structs. They just make the code easier to understand.

The screenshot below was created from the data only for illustration purposes.

int numberOfCoordinates = 10;

//This example draws a crude polygon with 10 coordinates
//around Algonquin Provincial Park.  Use as many coordinates
//as you like to achieve the accuracy you require.
CLLocationCoordinate2D algonquinParkCoordinates[numberOfCoordinates];
algonquinParkCoordinates[0] = CLLocationCoordinate2DMake(46.105,    -79.4);
algonquinParkCoordinates[1] = CLLocationCoordinate2DMake(46.15487,  -78.80759);
algonquinParkCoordinates[2] = CLLocationCoordinate2DMake(46.16629,  -78.12095);
algonquinParkCoordinates[3] = CLLocationCoordinate2DMake(46.11964,  -77.70896);
algonquinParkCoordinates[4] = CLLocationCoordinate2DMake(45.74140,  -77.45627);
algonquinParkCoordinates[5] = CLLocationCoordinate2DMake(45.52630,  -78.22532);
algonquinParkCoordinates[6] = CLLocationCoordinate2DMake(45.18662,  -78.06601);
algonquinParkCoordinates[7] = CLLocationCoordinate2DMake(45.11689,  -78.29123);
algonquinParkCoordinates[8] = CLLocationCoordinate2DMake(45.42230,  -78.69773);
algonquinParkCoordinates[9] = CLLocationCoordinate2DMake(45.35672,  -78.90647);

//Create CGPath from the above coordinates...
CGMutablePathRef mpr = CGPathCreateMutable();

for (int p=0; p < numberOfCoordinates; p++)
{
    CLLocationCoordinate2D c = algonquinParkCoordinates[p];

    if (p == 0)
        CGPathMoveToPoint(mpr, NULL, c.longitude, c.latitude);
    else
        CGPathAddLineToPoint(mpr, NULL, c.longitude, c.latitude);
}

//set up some test coordinates and test them...
int numberOfTests = 7;
CLLocationCoordinate2D testCoordinates[numberOfTests];
testCoordinates[0] = CLLocationCoordinate2DMake(45.5, -78.5);
testCoordinates[1] = CLLocationCoordinate2DMake(45.3, -79.1);
testCoordinates[2] = CLLocationCoordinate2DMake(45.1, -77.9);
testCoordinates[3] = CLLocationCoordinate2DMake(47.3, -79.6);
testCoordinates[4] = CLLocationCoordinate2DMake(45.5, -78.7);
testCoordinates[5] = CLLocationCoordinate2DMake(46.8, -78.4);
testCoordinates[6] = CLLocationCoordinate2DMake(46.1, -78.2);

for (int t=0; t < numberOfTests; t++)
{
    CGPoint testCGPoint = CGPointMake(testCoordinates[t].longitude, testCoordinates[t].latitude);

    BOOL tcInPolygon = CGPathContainsPoint(mpr, NULL, testCGPoint, FALSE);

    NSLog(@"tc[%d] (%f,%f) in polygon = %@",
          t,
          testCoordinates[t].latitude,
          testCoordinates[t].longitude,
          (tcInPolygon ? @"Yes" : @"No"));
}

CGPathRelease(mpr);

Here are the results of the above test:

tc[0] (45.500000,-78.500000) in polygon = Yes
tc[1] (45.300000,-79.100000) in polygon = No
tc[2] (45.100000,-77.900000) in polygon = No
tc[3] (47.300000,-79.600000) in polygon = No
tc[4] (45.500000,-78.700000) in polygon = Yes
tc[5] (46.800000,-78.400000) in polygon = No
tc[6] (46.100000,-78.200000) in polygon = Yes


This screenshot is to illustrate the data only (actual MKMapView is not required to run the code above):

这篇关于确定CLLocationCoordinate2D是否在定义的区域(边界)内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆