2013-02-01 33 views
0

在我的应用程序中,我有一个圆周的坐标数组(CLLocationCoordinate2D)。现在我从某处获得坐标,并且需要检查新坐标是否落在圆内。 如果是这样,我只是想在该坐标上显示一个针,并且基本上它应该落在该区域下。 我该如何去检查这个?如何检测GPS坐标是否落在MKMapView的某个区域?

感谢,

回答

3

你没有说,如果你的坐标阵列只是纬度和经度,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]; 

与你离线聊天,这听起来像在地图上的坐标的阵列是一个用户手势的结果(这可以解释为什么你没” t具有坐标圆的中心)。

鉴于此情况,而不是使用映射方法试图找出区域是否包含坐标,我建议使用Quartz方法来测试视图中的CGPoint是否包含在关闭UIBezierPath,我们将从用户的手势构建。

因此:

  • 构建UIBezierPath用户拖动在屏幕上它们的手指;

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

  • 具有CGPoint的MapView用户的当前位置内,我们现在可以使用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]; 
    } 
} 
+0

对不起that.Its CLLocationCoordniate2D。 – Ashutosh

+0

我没有中心的坐标。即使我得到了距离,我怎么知道它是否在边界之下。请解释我知道你要去哪里,但仍然无法在代码中想到它。 – Ashutosh

+0

是的,我的意思是在圈子的外围。 – Ashutosh

相关问题