通过按下同一按钮来切换MapKit叠加层的开/关? [英] Toggling MapKit Overlays On/Off by pressing the same button?

查看:81
本文介绍了通过按下同一按钮来切换MapKit叠加层的开/关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有工具栏按钮的MapView,按下该按钮可将叠加层添加到MapView.我想要的是按钮(IBAction),以检查地图上是否已经有叠加层,是否有要删除的叠加层,如果没有,则添加它们.

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");
}
}

此代码一次可以添加叠加层,一次可以删除叠加层.之后(从查看Log输出中)似乎函数(按钮)认为MapView仍在其上,因此它需要继续删除它们(即使它们不再存在了).

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).

在此先感谢您的帮助!

推荐答案

请改为尝试检查overlays数组的count:

Try checking the count of the overlays array instead:

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

如果不检查count,则overlays数组可以为非零,但没有对象.

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

这篇关于通过按下同一按钮来切换MapKit叠加层的开/关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆