【问题标题】:check current location is in MkPolygons检查当前位置是否在 MkPolygons
【发布时间】:2016-03-23 03:58:20
【问题描述】:

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

如何解决这个问题?

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

【问题讨论】:

  • 或许这里的答案会对你有所帮助:stackoverflow.com/questions/4354130/…
  • 我无法复制它。我用你的代码检查了一些多边形,我总是只得到一个多边形的 TRUE。

标签: iphone ios7 map mkpolygon


【解决方案1】:

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)

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-09
    • 1970-01-01
    • 1970-01-01
    • 2014-08-04
    相关资源
    最近更新 更多