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如果第一个多边形在 location 内。

如何解决这个问题?

如果有人知道,请给我一个建议

4

1 回答 1

0

Swift很简单,你需要做如下,注意例子中没有实现MKPolygon Array for mutiple MKPolygon on a map

// Init array for any MKPolygons
var arrayMKPolygon : NSMutableArray = []

override func viewWillAppear(animated: Bool) {

    // Add one or more than one
    self.setMKPolygon()

    let tapGesture = UITapGestureRecognizer(target: self, action: "revealRegionDetailsWithPressOnMap:")
    tapGesture.numberOfTapsRequired = 1
    tapGesture.numberOfTouchesRequired = 1

    self.mapView.addGestureRecognizer(tapGesture)

}


func setMKPolygon(){

    // Poinst for polygon -> (or NSArray)
for() {
   ----> (dynamic for example web service) var points = [CLLocationCoordinate2DMake(41.000512, -109.050116),
        CLLocationCoordinate2DMake(41.002371, -102.052066),
        CLLocationCoordinate2DMake(36.993076, -102.041981),
        CLLocationCoordinate2DMake(36.99892, -109.045267)]

    // Polygon
    let poly: MKPolygon = MKPolygon(coordinates: &points, count: 4)

    // Add polygon
    mapView.addOverlay(poly)
    // Add objecto to Array 
    arrayMKPolygon.addObject(poly)
  }

}

func revealRegionDetailsWithPressOnMap(sender: UITapGestureRecognizer) {

    let touchLocation = sender.locationInView(self.mapView)

    let locationCoordinate = self.mapView.convertPoint(touchLocation, toCoordinateFromView: self.mapView)

    print("Tapped at lat: \(locationCoordinate.latitude) long: \(locationCoordinate.longitude)")

    for i in 0...arrayMKPolygon.count - 1 {

        let polygonView = MKPolygonRenderer(overlay: arrayMKPolygon[i] as! MKOverlay)

        let mapPoint = MKMapPointForCoordinate(locationCoordinate)

        let circlePoint = polygonView.pointForMapPoint(mapPoint)

        let mapCoordinateIsInCircle : Bool = CGPathContainsPoint(polygonView.path, nil, circlePoint, false)

        if mapCoordinateIsInCircle{
            print("Yes, is within index --> \(i)")
        }
        else{
            print("NO")
        }
    }
}
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {

    if overlay is MKPolygon {

        let polygonView = MKPolygonRenderer(overlay: overlay)

        polygonView.strokeColor = UIColor.magentaColor()

        polygonView.fillColor = UIColor.yellowColor().colorWithAlphaComponent(0.15)

        polygonView.lineWidth = 1.5;

        return polygonView

    }

    return MKPolygonRenderer(overlay: overlay)

}
于 2016-03-22T19:58:20.410 回答