通过按相同的按钮打开/关闭MapKit叠加?

时间:2011-07-22 17:49:25

标签: iphone mapkit overlay toggle ibaction

我有一个带有工具栏按钮的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仍然有覆盖它,因此它需要继续删除它们(即使它们不再存在)。

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

请尝试检查覆盖数组的count

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

不检查countoverlays数组可以不是零,但没有对象。