簡體   English   中英

如何檢測GPS坐標是否落在MKMapView的某個區域?

[英]How to detect if a GPS coordinate fall in an area on MKMapView?

在我的應用程序中,我有一個圓周長的坐標數組(CLLocationCoordinate2D)。 現在我從某個地方得到一個坐標,我需要檢查新坐標是否落在圓圈內。 如果它,我只是想在該坐標上顯示一個引腳,基本上它應該屬於該區域。 我應該如何檢查這個?

謝謝,

你沒有說你的坐標數組是否只是緯度和經度, 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];

離線聊天時,聽起來地圖上的坐標數組是用戶手勢的結果(這解釋了為什么你沒有坐標圓的中心)。

鑒於這種情況,而不是使用映射方法來試圖弄清楚一個區域是否包含一個坐標,我建議使用Quartz方法來測試視圖中的CGPoint是否包含在一個封閉的UIBezierPath ,我們將從用戶的手勢構建。

從而:

  • 當用戶在屏幕上拖動手指時,構建UIBezierPath ;

  • 完成后,將結果路徑與相關坐標進行比較。 例如,地圖視圖顯示用戶位置,您可以查看地圖的userLocation屬性,使用地圖視圖的convertCoordinate:toPointToView方法將其坐標從CLLocationCoordinate2D轉換為視圖中的坐標視圖。

  • CGPoint放在mapview中用於用戶的當前位置,我們現在可以使用UIBezierPath實例方法containsPoint進行測試,以查看該點是否在UIBezierPath路徑內。

因此,這可能看起來像:

- (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];
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM