2

MKPolygonがMKCircleと交差するかどうかを調べるのに苦労しています。

これが私の状況です。私は地域でいっぱいの地図を持っています。これで、ユーザーは地図上にピンを設定できます。そこから、ピンの位置を中心としてMKCircleを描画します。ここで、この円がすでにマップ上の一部の領域と重なっているかどうかを知りたいと思います。

私の考えは、CGPathContainsPointメソッドを使用して、ポリゴンのすべてのポイントが円内にあるかどうかを確認することです。

これは私のコードです:

//the circle for the location filter
    MKCircleView *circleView = (MKCircleView *)[map viewForOverlay:self.sfvc.pinCircle];


//loop all regions
for (MKPolygon *region in regions){

    BOOL mapCoordinateIsInPolygon = FALSE;

    //loop all point of this region
    for (int i = 0; i < region.pointCount; i++) {
       MKMapPoint point = region.points[i];
       CGPoint circleViewPoint = [circleView pointForMapPoint:point];
       mapCoordinateIsInPolygon = CGPathContainsPoint(circleView.path, NULL, circleViewPoint, NO);
    }

    if (mapCoordinateIsInPolygon) {
        NSLog(@"YES! At least one point of the poly lies within the circle");
    }

}

残念ながら、予測できない結果が得られますが、それは意味がありません。私が間違っていることについて何か考えはありますか?私がやりたいことをする別の方法はありますか?

私のコードの一部は、アノテーションがMKPolygonView(iOS)内にあるかどうかを判断する方法からのものです。

注:私のソリューションは、領域/パスに十分なポイントが定義されているため、少なくとも1つのポイントが円に含まれるという前提に基づいていることを知っています。

前もって感謝します、

乾杯、パウィ

4

2 に答える 2

3

さて、ようやくわかりました。上記のコードは、ループ内に欠落しているbreakステートメントがあることを除いて、そのまま機能するはずです。そのままのコードは、ポリゴンの最後にチェックされたポイントのみを返します。mapCoordinateIsInPolygonにテストを挿入してから、内側のループにbreakステートメントを挿入すると、最初のテストが陽性になるとすぐにループが終了し、正しい結果が得られます。;-)

于 2011-08-05T06:50:50.027 に答える
0
CLLocationCoordinate2D newCoord ;
            newCoord.latitude = [[firDict objectForKey:@"Latitude"] floatValue];
            newCoord.longitude = [[firDict objectForKey:@"Longitude"] floatValue];

            [self pointInsideOverlay:newCoord MyOverlay:Curoverlay];

-(void)pointInsideOverlay:(CLLocationCoordinate2D )tapPoint MyOverlay:(id <MKOverlay>)overlay
{
    isInside = FALSE;
    MKMapPoint mapPoint = MKMapPointForCoordinate(tapPoint);
    CGMutablePathRef mpr = CGPathCreateMutable();
    MKMapPoint *polygonPoints = myPolygon.points;
    for (int p=0; p < myPolygon.pointCount; p++)
    {
        MKMapPoint mp = polygonPoints[p];
        if (p == 0)
            CGPathMoveToPoint(mpr, NULL, mp.x, mp.y);
        else
            CGPathAddLineToPoint(mpr, NULL, mp.x, mp.y);
    }

    CGPoint mapPointAsCGP = CGPointMake(mapPoint.x, mapPoint.y);
    BOOL pointIsInPolygon = CGPathContainsPoint(mpr, NULL, mapPointAsCGP, FALSE);
    if (pointIsInPolygon)
    {
        isInside = TRUE;
    }

    CGPathRelease(mpr);
}
于 2015-02-12T05:01:57.007 に答える