如果位置到达自定义区域,如何执行操作

时间:2014-03-31 13:29:53

标签: ios

我需要一些帮助,将zipcode或当前位置值与自定义区域的响应操作相结合。以为它被称为多边形类。如果我错了,请纠正我。

例如: 我有以下这些位置。如果用户输入位于以下位置。必须采取行动。

(52.38434879837112, 4.882092475891113)(52.38036714957116, 4.888787269592285)(52.37964674712061, 4.890203475952148)(52.37891323436772, 4.893829822540283)(52.38018377551705, 4.895482063293457)(52.38163764888611, 4.891576766967773)(52.38241040893942, 4.889967441558838)(52.38284262476619, 4.890375137329102)(52.384977446937405, 4.890353679656982)(52.38694191601119, 4.890375137329102)(52.38828425293476, 4.890316128730774)(52.38823841771291, 4.889897704124451)(52.388035432586925, 4.886292815208435)(52.38811400758514, 4.885584712028503)(52.38819585639288, 4.884763956069946)(52.3877014872842, 4.884393811225891)(52.386264184057815, 4.88343358039856)(52.386120123751006, 4.883519411087036)(52.38551768464715, 4.883063435554504)(52.38496107599452, 4.882376790046692)(52.3845125097973, 4.882033467292786)

Wat是xcode的可能性吗?或者我是否需要在其他编码语言中执行此操作? 在HTML中执行此操作并在UIWebView中实现它可能很有用吗?

更新1:

我已经使用了Polygon Map。唯一的问题是,我不知道如何制作正确的注释并让它检查多边形区域。我得到MKMapPoint *mapPoint = MKMapPointForCoordinate(CLLocationCoordinate2DMake(52.366243, 4.891097));的语义问题,它说它是'MKMapPoint'的不兼容类型。

我做错了什么?

这是btw,地图中的1个多边形。 CGPathContainsPoint代码如何检查多个多边形?

    CLLocationCoordinate2D roodPoly2[8];

    roodPoly2[0] = CLLocationCoordinate2DMake(52.36715572007325, 4.889763593673706);
    roodPoly2[1] = CLLocationCoordinate2DMake(52.367037801571975, 4.889838695526123);
    roodPoly2[2] = CLLocationCoordinate2DMake(52.36681506576634, 4.891244173049927);
    roodPoly2[3] = CLLocationCoordinate2DMake(52.3667561061004, 4.893143177032471);
    roodPoly2[4] = CLLocationCoordinate2DMake(52.365314845350156, 4.892692565917969);
    roodPoly2[5] = CLLocationCoordinate2DMake(52.36555724249258, 4.890578985214233);
    roodPoly2[6] = CLLocationCoordinate2DMake(52.366729901779145, 4.888583421707153);
    roodPoly2[7] = CLLocationCoordinate2DMake(52.36707710777403, 4.889581203460693);

    MKPolygon *roodpoly2 = [MKPolygon polygonWithCoordinates:roodPoly2 count:8];
    roodpoly2.title = @"Dinsdag & Vrijdag";
    [myMapView addOverlay:roodpoly2];

    MKMapPoint *mapPoint = MKMapPointForCoordinate(CLLocationCoordinate2DMake(52.366243, 4.891097));

    CGMutablePathRef mpr = CGPathCreateMutable();

    MKMapPoint *polygonPoints = roodpoly2.points;
    //myPolygon is the MKPolygon

    for (int p=0; p < roodpoly2.pointCount; 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);
    //mapPoint above is the MKMapPoint of the coordinate we are testing.
    //Putting it in a CGPoint because that's what CGPathContainsPoint wants.

    BOOL pointIsInPolygon = CGPathContainsPoint(mpr, NULL, mapPointAsCGP, FALSE);

    CGPathRelease(mpr);

}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    if ([overlay isKindOfClass:[MKPolygon class]])
    {
        MKPolygonView* aView = [[MKPolygonView alloc] initWithPolygon:       (MKPolygon*)overlay];

        aView.fillColor = [[UIColor greenColor] colorWithAlphaComponent:0.2];
        aView.strokeColor = [[UIColor greenColor] colorWithAlphaComponent:0.7];
        aView.lineWidth = 3;

        return aView;
    }
    return nil;
}

2 个答案:

答案 0 :(得分:2)

首先,编译错误&#34;不兼容类型MKMapPoint&#34;在这一行:

MKMapPoint *mapPoint = MKMapPointForCoordinate(...);

是因为MKMapPointForCoordinate返回了一个普通的MKMapPoint结构,但您已将mapPoint声明为{em>指针到MKMapPoint结构中,所以它&#34;不兼容&#34;。

不要将mapPoint声明为指针(删除星号)。所以将该行更改为:

MKMapPoint mapPoint = MKMapPointForCoordinate(...);



下一步,您的另一个问题是&#34; CGPathContainsPoint代码如何检查多个多边形?&#34;

一种方式是将所有多边形的所有CGMutablePathRef路径存储在C数组中,就像您将各个多边形坐标存储在C数组中一样。

然后,您可以循环遍历路径数组,并为每个数组调用CGPathContainsPoint

<小时/> 以下是测试已添加到地图的多个多边形叠加层的示例。此示例假设您已使用MKPolygonaddOverlay向地图添加了addOverlays叠加层。

无论您想要测试某个坐标是否在添加到地图中的任何多边形中的任何位置,请输入:

CLLocationCoordinate2D coordinateToTest = 
    CLLocationCoordinate2DMake(52.366243, 4.891097);

MKMapPoint mapPointToTest = MKMapPointForCoordinate(coordinateToTest);

//Loop through MKPolygon overlays...
for (id<MKOverlay> overlay in myMapView.overlays)
{
    if ([overlay isKindOfClass:[MKPolygon class]])
    {
        MKPolygon *polygon = (MKPolygon *)overlay;


        CGMutablePathRef mpr = CGPathCreateMutable();

        MKMapPoint *polygonPoints = polygon.points;

        for (int p=0; p < polygon.pointCount; 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(mapPointToTest.x, mapPointToTest.y);

        BOOL pointIsInPolygon = CGPathContainsPoint(mpr, NULL, mapPointAsCGP, FALSE);
        NSLog(@"Coordinate %f,%f is in polygon %@: %@",
              coordinateToTest.latitude, coordinateToTest.longitude,
              polygon.title,
              (pointIsInPolygon ? @"Yes" : @"No"));

        CGPathRelease(mpr);
    }
}

答案 1 :(得分:0)

您在谈论什么系统框架?可能MKMapView?深入研究CLLocationCoordinate以及如何比较它们:

memcmp(&first_cllc2d, &second_cllc2d, sizeof(CLLocationCoordinate2D)) 

(fabs(first_cllc2d.latitude - second_cllc2d.latitude) <= epsilon && 
 fabs(first_cllc2d.longitude - second_cllc2d.longitude) <= epsilon)