首页 文章

MKAnnotation不会使用NSThread显示在 Map 中

提问于
浏览
3

我有一个方法在 Map 中插入注释 . 此方法由map的委托方法调用 .

问题是注释没有出现在 Map 中 . 我必须再次触摸 Map 以显示注释 .

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

代码上方:

映射委派方法:

- (void) mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{

    log(@"region changed");

    if (_userRegion.center.latitude) {
        log(@"Move map. New location: %f X %f", self.mapView.centerCoordinate.latitude, self.mapView.centerCoordinate.longitude);

        NSDictionary *coordinates = @{
                                      @"latitude": [NSString stringWithFormat:@"%f", self.mapView.centerCoordinate.latitude],
                                      @"longitude": [NSString stringWithFormat:@"%f", self.mapView.centerCoordinate.longitude]
                                      };
        NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(updateMap:) object:coordinates];
        [thread start];
    }

    if(_userMenuOpened){
        [self closeUserMenu];
    }

    if(_settingsMenuOpened){
        [self closeSettingsMenu];
    }

}

插入注释的方法:

- (void) updateMap:(NSDictionary *) coordinates {

    NSArray *annotationsInMap = [self.mapView annotations];

    _busStops = [_ws getStations:10 lat:[[coordinates valueForKey:@"latitude"] doubleValue] lng:[[coordinates valueForKey:@"longitude"] doubleValue]];

    for(NSString *stop in _busStops) {

        BOOL inMap = NO;
        BPBusStopAnnotation *annotation = [[BPBusStopAnnotation alloc] initWithData:stop];

        //Se tiver annotation no mapa verifica se os que vem no WS sao os mesmos
        if(annotationsInMap.count > 0){

            for(BPBusStopAnnotation *pt in annotationsInMap){
                if(pt && ![pt isKindOfClass:[MKUserLocation class]]){

                    if([annotation.codigo isEqual:pt.codigo]){
                        inMap = YES;
                        break;

                    }
                }
            }

        }

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

    }

    [CATransaction flush];

}

谢谢!!!

2 回答

  • 1

    你自己说:这是线程 . 除了主线程之外,你不能做任何影响接口的事情,这就是问题所在 .

    (当你解决这个问题时,如果我是你,我将永远不会使用NSThread . 这是所有可能的方法来进行iOS线程化 . )

  • 1

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

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

    对于

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

相关问题