【问题标题】:MKAnnotation don't appear in map using NSThreadMKAnnotation 不会出现在使用 NSThread 的地图中
【发布时间】:2014-01-10 17:51:51
【问题描述】:

我有一个在地图中插入注释的方法。该方法由map的委托方法调用。

问题是注释没有出现在地图中。我必须再触摸一次地图才能显示注释。

插入注释后,我正在使用[CATransaction flush],但它不起作用。

代码上方:

映射委托方法:

- (void) mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{ log(@"区域改变"); 如果(_userRegion.center.latitude){ log(@"移动地图。新位置:%f X %f", self.mapView.centerCoordinate.latitude, self.mapView.centerCoordinate.longitude); NSDictionary *坐标 = @{ @"latitude": [NSString stringWithFormat:@"%f", self.mapView.centerCoordinate.latitude], @"longitude": [NSString stringWithFormat:@"%f", self.mapView.centerCoordinate.longitude] }; NSThread *thread = [[NSThread alloc] initWithTarget:self 选择器:@selector(updateMap:) object:coordinates]; [线程开始]; } 如果(_userMenuOpened){ [自我关闭用户菜单]; } 如果(_settingsMenuOpened){ [自我关闭设置菜单]; } }

插入注解的方法:

- (void) updateMap:(NSDictionary *) 坐标 { NSArray *annotationsInMap = [self.mapView 注释]; _busStops = [_ws getStations:10 lat:[[coordinates valueForKey:@"latitude"] doubleValue] lng:[[coordinates valueForKey:@"longitude"] doubleValue]]; for(NSString *stop in _busStops) { 布尔 inMap = 否; BPBusStopAnnotation *annotation = [[BPBusStopAnnotation alloc] initWithData:stop]; //Se tiver annotation no mapa verifica se os que vem no WS sao os mesmos 如果(annotationsInMap.count > 0){ for(BPBusStopAnnotation *pt in annotationsInMap){ if(pt && ![pt isKindOfClass:[MKUserLocation 类]]){ if([annotation.codigo isEqual:pt.codigo]){ inMap = 是的; 休息; } } } } 如果(!inMap){ [self.mapView addAnnotation:annotation]; } } [CATransaction 刷新]; }

谢谢!!!

【问题讨论】:

标签: ios objective-c mapkit mkannotation nsthread


【解决方案1】:

在“updateMap”方法中,我改变了

if (!inMap) {
    [self.mapView addAnnotation:annotation];
}

[self.mapView performSelectorOnMainThread: @selector(addAnnotation:) withObject: annotation waitUntilDone: YES];

【讨论】:

    【解决方案2】:

    你自己说过:这是线程。除了主线程,你不能做任何影响界面的事情,这就是问题所在。

    (当你解决这个问题时,如果我是你,我将永远使用 NSThread。这是所有可能的 iOS 线程方法中最糟糕的。)

    【讨论】:

    • 感谢您的回答@matt,但我该如何解决我的问题?我需要在该委托方法中调用 ws,但如果我在主线程中这样做,它会冻结屏幕直到完成 ws 调用。
    • 当你想在后台做事时你会进入后台线程,当你想在主线程中做事时(例如更新界面)回到主线程。
    • 它可能会帮助您在继续之前停止并了解 iOS 中的线程。例如,请参阅the Threads chapter of my book
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多