从地图中用户放置的注释创建 MKPolygon
我希望用户能够在地图中放置一些(未知数量)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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一个问题是 addCorner 方法。它不是将每个角添加到 ptsArray 变量中,而是创建一个仅包含最后一个角的新数组,并将 ptsArray 设置为等于该数组,以便它只有最后一个角。
像这样更改
addCorner
方法:同时确保
ptsArray
已正确声明和合成:(顺便说一句,为什么不将角添加到
ptsArray
就在创建pointAnnotation
的位置,而不是在单独的用户操作中?)第二个问题出现在
addPolygonOverlay
方法中。您必须使用NSArray
count
属性来获取数组中的项目数。sizeof
函数返回传递的变量使用的物理内存的字节数。对于作为指针的 ptsArray ,它将返回 4。如果 ptsArray 的项少于 4,您将得到“索引超出范围”异常。所以
改成
另一件需要注意的重要事情是,多边形边将按照点在数组中的顺序绘制。如果用户不按顺时针或逆时针顺序添加角,多边形就会看起来很奇怪。您可以在用户添加/删除注释后立即重新创建多边形叠加,以便他们立即获得有关其外观的反馈。
First problem is the
addCorner
method. Instead of adding each corner to theptsArray
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:Also make sure
ptsArray
is declared and synthesized properly:(By the way, why not add the corner to
ptsArray
right where thepointAnnotation
is created instead of in a separate user action?)Second problem is in the
addPolygonOverlay
method. You have to use theNSArray
count
property to get the number of items in the array. Thesizeof
function returns the number of bytes of physical memory the passed variable uses. ForptsArray
which is a pointer, it will return 4. If theptsArray
has less than 4 items, you will get the "index out of range" exception.So change
to
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.