2011-07-01 11 views
2

MKポリゴンと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"); 
    } 

} 

は、残念ながら、私は、予期しない結果を得る - どんな意味がありません。私が間違っていることは何か考えていますか?私が欲しいことをする別の方法がありますか?

私のコードはHow to determine if an annotation is inside of MKPolygonView (iOS)

ノートから一部である:私は私の解決策は、地域/パスが少なくとも1点は円に落ちるように定義されて十分なポイントを持っているという仮定に依存していることを知っています。事前に

おかげで、

乾杯、pawi

+0

予測不可能なことはどういう意味ですか?円内にない点が円内に表示されていますか? –

+0

他の方法では、円内には何もない点が検出されます。たとえそれらが明確であっても... – pawi

+0

ソースをリンクするといいですか? – Pochi

答えて

3

まあ、私はついにそれを考え出しました。上記のコードは、ループ内にbreakステートメントがないことを除いて、そのまま動作するはずです。コードasは、ポリの最後にチェックされた点のみを返します。 mapCoordinateIsInPolygonにテストを挿入してから内側のループにbreakステートメントを挿入すると、最初のテストが肯定的になるとループが終了し、正しい結果が得られます。 ;-)

+0

正しい答えを反映するために質問を編集できますか? – teradyl

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); 
} 
関連する問題