检查当前位置是在MkPolygons

时间:2014-03-18 11:30:01

标签: iphone ios7 map mkpolygon

我在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。

如何解决此问题?

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

1 个答案:

答案 0 :(得分:0)

Swift 很简单,您需要执行以下操作,示例中的注意未实现 >地图上的多个MKPolygon

// 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)

}