检查当前位置在MkPolygons中 [英] check current location is in MkPolygons

查看:63
本文介绍了检查当前位置在MkPolygons中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在IOS 7项目中工作,它包含位置检查(当前位置在给定的多边形中).

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

创建了一个MKPolygons数组

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
    }
  }

此代码对于第一个多边形正常工作(pointIsInPolygon正确返回YES/NO).然后下一次迭代(数组中的下一个多边形)pointIsInPolygon给出先前的状态均值,如果第一个多边形在位置之外,则返回NO如果第一个多边形在该位置之内,则返回YES.

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 .

如何解决此问题?

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

If anybody know, please give me a suggestion

推荐答案

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

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)

}

这篇关于检查当前位置在MkPolygons中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆