check current location is in MkPolygons

爷,独闯天下 提交于 2021-02-10 18:35:46

问题


I am working in an IOS 7 project ,it contains a location checking (current location is in given polygons).

I am Using the following code to check the condition

Created an array of 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
    }
  }

This code is working correctly(pointIsInPolygon return YES/NO correctly) for the first polygon .Then the next iteration (Next polygon from array) pointIsInPolygon gives the previous state means, it return NO if the first polygon was outside the location and it return YES if the first polygon was inside the location .

How to fix this issue?

If anybody know, please give me a suggestion


回答1:


Swift is simple, you need to do the following, attention in the example is not implemented 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)

}


来源:https://stackoverflow.com/questions/22478025/check-current-location-is-in-mkpolygons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!