gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 21:06:13 24 4
gpt4 key购买 nike

我有一个带有工具栏按钮的 MapView,按下该按钮时会向 MapView 添加叠加层。我想要的是按钮(IBAction)检查 map 上是否已经有叠加层,如果有则删除,如果没有则添加它们。

我当前添加叠加层的代码如下:

- (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 上仍然有覆盖层,因此需要不断删除它们(即使它们不再存在)。

预先感谢您的帮助!

最佳答案

尝试检查覆盖数组的计数:

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

在不检查count的情况下,overlays数组可以不为零,但没有对象。

关于iphone - 按同一个按钮可以打开/关闭 MapKit 叠加吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6794024/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com