按同一个按钮可以打开/关闭 MapKit 叠加吗?

发布于 2024-11-25 21:27:24 字数 1701 浏览 1 评论 0原文

我有一个带有工具栏按钮的 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 技术交流群。

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

发布评论

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

评论(1

少年亿悲伤 2024-12-02 21:27:24

尝试检查overlays数组的count

if ([[mapView overlays] count] > 0) {

如果不检查countoverlays数组可能不为nil,但没有对象。

Try checking the count of the overlays array instead:

if ([[mapView overlays] count] > 0) {

Without checking the count, the overlays array can be not-nil but have no objects.

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