按同一个按钮可以打开/关闭 MapKit 叠加吗?
我有一个带有工具栏按钮的 MapView,按下该按钮时会向 MapView 添加叠加层。我想要的是按钮(IBAction)检查地图上是否已经有覆盖物,如果有,则删除,如果没有,则添加它们。
我当前添加覆盖层的代码如下:
- (IBAction)waterWaysAction:(id)sender
{
NSLog(@"WaterWays pushed");
if ([mapView overlays]) {
[mapView removeOverlays:[mapView overlays]];
NSLog(@"WaterWays removed");
} else {
// ******* adds the overlays for the waterways **********
// inner harbor
CLLocationCoordinate2D innerHarborPoints[13] = {
CLLocationCoordinate2DMake(43.02313691051886, -87.90539558418189),
CLLocationCoordinate2DMake(43.0213450482963, -87.90596442438722),
CLLocationCoordinate2DMake(43.01721422337822, -87.90249007832719),
CLLocationCoordinate2DMake(43.0141641230024, -87.90402523886414),
CLLocationCoordinate2DMake(43.00858391833174, -87.8971780500095),
CLLocationCoordinate2DMake(43.016711699807, -87.90156448365555),
CLLocationCoordinate2DMake(43.01692320142091, -87.90093306118753),
CLLocationCoordinate2DMake(43.02204743639911, -87.90385746629964),
CLLocationCoordinate2DMake(43.02400128319255, -87.90186558765494),
CLLocationCoordinate2DMake(43.02441284233703, -87.89897827382163),
CLLocationCoordinate2DMake(43.02564995691736, -87.89925323299293),
CLLocationCoordinate2DMake(43.02549123239004, -87.90378517804325),
CLLocationCoordinate2DMake(43.02313691051886, -87.90539558418189)};
MKPolygon *innerHarborPolygon = [MKPolygon polygonWithCoordinates:innerHarborPoints count:13];
innerHarborPolygon.title = @"Inner Harbor";
[mapView addOverlay:innerHarborPolygon];
NSLog(@"WaterWays added");
}
}
此代码一次用于添加覆盖层,一次用于删除它。之后(从查看日志输出)看来功能(按钮)认为 MapView 上仍然有覆盖层,因此需要不断删除它们(即使它们不再存在)。
预先感谢您的任何帮助!
I have a MapView with a toolbar button that when pushed adds overlays to the MapView. What I would like is for the button (IBAction) to check to see if there already are overlays on the map and if there are remove, if there are not, to add them.
My current code that adds the overlays is as follows:
- (IBAction)waterWaysAction:(id)sender
{
NSLog(@"WaterWays pushed");
if ([mapView overlays]) {
[mapView removeOverlays:[mapView overlays]];
NSLog(@"WaterWays removed");
} else {
// ******* adds the overlays for the waterways **********
// inner harbor
CLLocationCoordinate2D innerHarborPoints[13] = {
CLLocationCoordinate2DMake(43.02313691051886, -87.90539558418189),
CLLocationCoordinate2DMake(43.0213450482963, -87.90596442438722),
CLLocationCoordinate2DMake(43.01721422337822, -87.90249007832719),
CLLocationCoordinate2DMake(43.0141641230024, -87.90402523886414),
CLLocationCoordinate2DMake(43.00858391833174, -87.8971780500095),
CLLocationCoordinate2DMake(43.016711699807, -87.90156448365555),
CLLocationCoordinate2DMake(43.01692320142091, -87.90093306118753),
CLLocationCoordinate2DMake(43.02204743639911, -87.90385746629964),
CLLocationCoordinate2DMake(43.02400128319255, -87.90186558765494),
CLLocationCoordinate2DMake(43.02441284233703, -87.89897827382163),
CLLocationCoordinate2DMake(43.02564995691736, -87.89925323299293),
CLLocationCoordinate2DMake(43.02549123239004, -87.90378517804325),
CLLocationCoordinate2DMake(43.02313691051886, -87.90539558418189)};
MKPolygon *innerHarborPolygon = [MKPolygon polygonWithCoordinates:innerHarborPoints count:13];
innerHarborPolygon.title = @"Inner Harbor";
[mapView addOverlay:innerHarborPolygon];
NSLog(@"WaterWays added");
}
}
This code works one time to add the overlay, and one time to remove it. After that (from viewing Log output) it appears as though the function (button) thinks the MapView still has overlays on it, and therefore it needs to keep removing them (even though they aren't there anymore).
Thanks in advance for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试检查overlays数组的
count
:如果不检查
count
,overlays
数组可能不为nil,但没有对象。Try checking the
count
of the overlays array instead:Without checking the
count
, theoverlays
array can be not-nil but have no objects.