从地图中用户放置的注释创建 MKPolygon

发布于 2024-12-12 18:09:39 字数 758 浏览 1 评论 0原文

我希望用户能够在地图中放置一些(未知数量)MKpointAnnotations 后创建多边形。我已经放置了一个手势识别器,一旦用户点击按钮,该识别器就会被激活,因此会放置注释。但是如何将它们用作MKPolygon 的角? 下面是用于保存多边形角点的代码。这是在我对其进行了一些修改之后。现在应用程序崩溃了,崩溃报告者说索引超出范围。角点是通过 GestureRecognizer 创建的 MKPointAnnotation。

-(IBAction)addCorner:(id)sender
{
NSMutableArray *addCorners = [[NSMutableArray alloc] init];
[addCorners addObject:pointAnnotation];
ptsArray = addCorners;
}

-(IBAction)addPolygonOverlay:(id)sender
{
int cornersNumber = sizeof(ptsArray);
MKMapPoint points[cornersNumber];

for (int i=0; i<cornersNumber; i++) {
    points[i] = MKMapPointForCoordinate([[ptsArray objectAtIndex:i] coordinate]);
}

MKPolygon *polygon = [MKPolygon polygonWithPoints:points count:cornersNumber];
[mapview addOverlay:polygon];

}

I want the user to be able to create polygons after placing some (unknown number) MKpointAnnotations in the map.I have put a gesture recognizer that gets activated once the user taps a button, and so annotations are placed.But how to use these as corners for a MKPolygon?
Below the code for saving the corners of the polygon.This after some mods I did to it.Now the app crashes and the crash reporter says index out of range.The corners are MKPointAnnotation-s created via a GestureRecognizer.

-(IBAction)addCorner:(id)sender
{
NSMutableArray *addCorners = [[NSMutableArray alloc] init];
[addCorners addObject:pointAnnotation];
ptsArray = addCorners;
}

-(IBAction)addPolygonOverlay:(id)sender
{
int cornersNumber = sizeof(ptsArray);
MKMapPoint points[cornersNumber];

for (int i=0; i<cornersNumber; i++) {
    points[i] = MKMapPointForCoordinate([[ptsArray objectAtIndex:i] coordinate]);
}

MKPolygon *polygon = [MKPolygon polygonWithPoints:points count:cornersNumber];
[mapview addOverlay:polygon];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甩你一脸翔 2024-12-19 18:09:39

第一个问题是 addCorner 方法。它不是将每个角添加到 ptsArray 变量中,而是创建一个仅包含最后一个角的新数组,并将 ptsArray 设置为等于该数组,以便它只有最后一个角。

像这样更改 addCorner 方法:

-(IBAction)addCorner:(id)sender
{
    if (ptsArray == nil)
    {
        self.ptsArray = [NSMutableArray array];
    }

    [ptsArray addObject:pointAnnotation];
}

同时确保 ptsArray 已正确声明和合成:(

//in the .h file...
@property (nonatomic, retain) NSMutableArray *ptsArray;
//in the .m file...
@synthesize ptsArray;

顺便说一句,为什么不将角添加到 ptsArray就在创建 pointAnnotation 的位置,而不是在单独的用户操作中?)

第二个问题出现在 addPolygonOverlay 方法中。您必须使用 NSArray count 属性来获取数组中的项目数。 sizeof 函数返回传递的变量使用的物理内存的字节数。对于作为指针的 ptsArray ,它将返回 4。如果 ptsArray 的项少于 4,您将得到“索引超出范围”异常。

所以

int cornersNumber = sizeof(ptsArray);

改成

int cornersNumber = ptsArray.count;

另一件需要注意的重要事情是,多边形边将按照点在数组中的顺序绘制。如果用户不按顺时针或逆时针顺序添加角,多边形就会看起来很奇怪。您可以在用户添加/删除注释后立即重新创建多边形叠加,以便他们立即获得有关其外观的反馈。

First problem is the addCorner method. Instead of adding each corner to the ptsArray variable, it creates a new array with just the last corner and sets theptsArray equal to that so it only has the one, last corner.

Change the addCorner method like this:

-(IBAction)addCorner:(id)sender
{
    if (ptsArray == nil)
    {
        self.ptsArray = [NSMutableArray array];
    }

    [ptsArray addObject:pointAnnotation];
}

Also make sure ptsArray is declared and synthesized properly:

//in the .h file...
@property (nonatomic, retain) NSMutableArray *ptsArray;
//in the .m file...
@synthesize ptsArray;

(By the way, why not add the corner to ptsArray right where the pointAnnotation is created instead of in a separate user action?)

Second problem is in the addPolygonOverlay method. You have to use the NSArray count property to get the number of items in the array. The sizeof function returns the number of bytes of physical memory the passed variable uses. For ptsArray which is a pointer, it will return 4. If the ptsArray has less than 4 items, you will get the "index out of range" exception.

So change

int cornersNumber = sizeof(ptsArray);

to

int cornersNumber = ptsArray.count;

Another important thing to note is that the polygon sides will be drawn in the order the points are in the array. If the user does not add corners in a clockwise or counter-clockwise order, the polygon will look strange. You could re-create the polygon overlay immediately after a user adds/removes an annotation so they get immediate feedback on how it will look.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文