1

私は IOS 7 プロジェクトで作業していますが、位置チェックが含まれています (現在の位置は指定されたポリゴンにあります)。

次のコードを使用して状態を確認しています

MKPolygons の配列を作成しました

for(MKPolygon *poly in self.polygonArray)
    {
        [self checkTheLocationIsInPolygon:currentLocation polygon:poly];
    }


- (void)checkTheLocationIsInPolygon:(CLLocation*)aLocation polygon:(MKPolygon*)aPolygon
{
    CLLocationCoordinate2D coordinate = {aLocation.coordinate.latitude, aLocation.coordinate.longitude};
    MKMapPoint mapPoint = MKMapPointForCoordinate(coordinate);

    CGMutablePathRef mpr = CGPathCreateMutable();

    MKMapPoint *polygonPoints = aPolygon.points;
    size_t nCount = aPolygon.pointCount;

    for (int p = 0; p < nCount; 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);
    CGPathRelease(mpr);


    if(pointIsInPolygon == YES)
    {
      //IN
    }
    else
    {
       //Out
    }
  }

このコードは、最初のポリゴンに対して正しく機能しています (pointIsInPolygon は正しく YES/NO を返します)。次に、次の反復 (配列からの次のポリゴン) pointIsInPolygon は前の状態の意味を示します。最初のポリゴンが場所の外にあった場合は NO を返し、YES を返します。最初のポリゴンが場所の内側にあった場合。

この問題を解決するにはどうすればよいですか?

知ってる人いたらアドバイスお願いします

4

1 に答える 1