1

在我的应用程序中,我有一个圆周长的坐标数组(CLLocationCoordinate2D)。现在我从某个地方得到一个坐标,我需要检查新坐标是否落在圆圈内。如果确实如此,我只想在该坐标上显示一个图钉,基本上它应该落在该区域之下。我应该如何检查这个?

谢谢,

4

1 回答 1

4

您没有说您的坐标数组是否只是纬度和经度、CLLocationCoordinate2D结构或CLLocation对象,但是如果您创建了一个CLLocation对象(如果您还没有对象),那么您可以调用distanceFromLocation以查看它离它有多远另一个位置。我假设,除了周长的坐标数组之外,您还有中心的坐标?

如果你有CLLocationCoordinate2D,你可以这样做:

CLLocation *location = [[CLLocation alloc] initWithLatitude:coordinate.latitude
                                                  longitude:coordinate.longitude];

CLLocation *centerLocation = [[CLLocation alloc] initWithLatitude:centerCoordinate.latitude
                                                        longitude:centerCoordinate.longitude];

CLLocationDistance distance = [location distanceFromLocation:centerLocation];

离线与您聊天,听起来地图上的坐标数组是用户手势的结果(这解释了为什么您没有坐标圆的中心)。

CGPoint鉴于这种情况,我建议使用 Quartz 方法测试视图中的 a 是否包含在 closed 中,而不是使用映射方法来尝试确定区域是否包含坐标,UIBezierPath我们将从用户的手势构造。

因此:

  • UIBezierPath当用户在屏幕上拖动手指时构建一个;

  • 完成后,将生成的路径与相关坐标进行比较。例如,地图视图正在显示用户位置,您可以查看地图的userLocation属性,CLLocationCoordinate2D使用地图视图的convertCoordinate:toPointToView方法将其坐标从 a 转换为视图中的视图坐标)。

  • 有了CGPoint用户当前位置的地图视图内,我们现在可以使用UIBezierPath实例方法containsPoint来测试该点是否在贝塞尔路径内。

因此,这可能看起来像:

- (void)turnOnGestureForView:(MKMapView *)mapView
{
    mapView.scrollEnabled = NO;

    UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
    [mapView addGestureRecognizer:gesture];
}

- (void)turnOffGesture:(UIGestureRecognizer *)gesture map:(MKMapView *)mapView
{
    [mapView removeGestureRecognizer:gesture];
    mapView.scrollEnabled = YES;
}

- (void)handleGesture:(UIPanGestureRecognizer *)gesture
{
    static UIBezierPath *path;
    static CAShapeLayer *shapeLayer;

    CGPoint location = [gesture locationInView:gesture.view];

    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        if (!shapeLayer)
        {
            shapeLayer = [[CAShapeLayer alloc] init];
            shapeLayer.fillColor = [[UIColor clearColor] CGColor];
            shapeLayer.strokeColor = [[UIColor redColor] CGColor];
            shapeLayer.lineWidth = 3.0;
            [self.mapView.layer addSublayer:shapeLayer];
        }
        path = [UIBezierPath bezierPath];
        [path moveToPoint:location];
    }
    else if (gesture.state == UIGestureRecognizerStateChanged)
    {
        [path addLineToPoint:location];
        shapeLayer.path = [path CGPath];
    }
    else if (gesture.state == UIGestureRecognizerStateEnded)
    {
        MKMapView *mapView = (MKMapView *)gesture.view;

        [path addLineToPoint:location];
        [path closePath];
        CGPoint currentLocation = [mapView convertCoordinate:mapView.userLocation.coordinate
                                                    toPointToView:gesture.view];
        if ([path containsPoint:currentLocation])
            NSLog(@"%s path contains %@", __FUNCTION__, NSStringFromCGPoint(currentLocation));
        else
            NSLog(@"%s path does not contain %@", __FUNCTION__, NSStringFromCGPoint(currentLocation));

        [shapeLayer removeFromSuperlayer];
        shapeLayer = nil;

        // if you want to turn off the gesture and turn scrolling back on, you can do that now

        [self turnOffGesture:gesture map:mapView];
    }
}
于 2013-02-01T20:36:10.947 回答