1

我每次訪問視圖時都會將我的mapView設置爲默認位置。有時當你訪問視圖時,它將處於正確的位置,儘管有時當你訪問視圖時,它將位於非洲南部海洋上的mapView默認位置。如何在訪問視圖時確保它處於正確的位置。iOS mapView有時不會加載到默認位置

這裏是我的代碼:

-(void)viewDidLoad { 

    [super viewDidLoad]; 

    self.mapView.delegate = self; 

    MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } }; 
    region.center.latitude = 45.442424; 
    region.center.longitude = -122.78; 
    region.span.latitudeDelta = 0.60; 
    region.span.longitudeDelta = 0.60; 
    [mapView setRegion:region animated:YES]; 
} 

這是加載註釋:

-(void)initXML { 

    NSURL *theURL = [NSURL URLWithString:@"http://www.wccca.com/PITS/"]; 
    NSData *data = [[NSData alloc] initWithContentsOfURL:theURL]; 
    xpathParser = [[TFHpple alloc] initWithHTMLData:data]; 
    NSArray *elements = [xpathParser searchWithXPathQuery:@"//input[@id='hidXMLID']//@value"]; 
    if (elements.count >= 1) { 

     TFHppleElement *element = [elements objectAtIndex:0]; 
     TFHppleElement *child = [element.children objectAtIndex:0]; 
     NSString *idValue = [child content]; 

     NSString *idwithxml = [idValue stringByAppendingFormat:@".xml"]; 
     NSString *url = @"http://www.wccca.com/PITS/xml/fire_data_"; 
     NSString *finalurl = [url stringByAppendingString:idwithxml]; 

     xmlParser = [[XMLParser alloc] loadXMLByURL:finalurl]; 

     if (xmlParser.calls.count == 0) { 

      [self noCallsMessage]; 
     } 
     else { 

      [self wcccaAnn]; 

      NSArray *callsArray = [xmlParser calls]; 

      for (JointCAD *call in callsArray) { 
       NSString *callnumber = [call.callnumber stringByAppendingFormat:@". "]; 
       NSString *callandnumber = [callnumber stringByAppendingString:call.currentCallType]; 
       Annotation *ann = [[Annotation alloc] init]; 
       ann.title = callandnumber; 
       ann.subtitle = [call location]; 
       ann.coordinate = CLLocationCoordinate2DMake([call.latitude doubleValue], [call.longitude doubleValue]); 
       [mapView addAnnotation:ann]; } 
      } 
     } 
    } 

MKAnnotationView:

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { 

    if ([annotation isKindOfClass:[MKUserLocation class]]) 
     return nil; 

    MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"current"]; 
    MyPin.pinColor = MKPinAnnotationColorRed; 

    MyPin.draggable = NO; 
    MyPin.highlighted = YES; 
    MyPin.animatesDrop= YES; 
    MyPin.canShowCallout = YES; 

    return MyPin; 
} 
+1

所有的UI操作應該在主線程上執行 – NeverBe

+0

這個視圖中的其他位置除了viewDidLoad被調用setRegion嗎? – Anna

+0

唯一的另一次是當用戶按下按鈕和mapView.showsUserLocation = YES;並且地圖放大到用戶位置。 –

回答

0

您是在比不同的隊列更新地圖主隊列。你很幸運,你的應用程序並沒有崩潰。將視圖操作移至主線程,看看是否繼續出現相同的問題。

編輯:如果你真的要使用隊列來獲取跨度,只是這樣做這樣的觀點:

dispatch_async(dispatch_get_main_queue(), ^{ 
    [mapView setRegion:region animated:YES]; 
}); 
+0

但是,地圖加載到它的默認視圖,而不是我指定的視圖。 –

+0

您是否已將更新從地圖視圖位置從後臺線程移除到主線程? – J2theC

+0

是的,我做了,我也把它放在viewDidLoad,它仍然是一半和一半。 –

0

有一個在你上面的代碼中設置地圖的地圖只有一個點位置(setRegion)。如果您的地圖被設置爲不同的位置,則不在您提供的代碼中。

+0

並不是說它被移動到了不同的地區,而是因爲地圖不像上面的代碼那樣移動。 –

1

我有同樣的問題,以下解決方案(適用於您的代碼)爲我工作:

-(void)viewDidLoad { 

    [super viewDidLoad]; 

    self.mapView.delegate = self; 

    double delayInSeconds = 0.5; 
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, int64_t)(delayInSeconds * NSEC_PER_SEC)); 
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 
     MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } }; 
     region.center.latitude = 45.442424; 
     region.center.longitude = -122.78; 
     region.span.latitudeDelta = 0.60; 
     region.span.longitudeDelta = 0.60; 
     [mapView setRegion:region animated:YES]; 
    }); 
} 

基本上,你換行設定區域的塊中的代碼,並調用該塊delayInSeconds後。我在模擬器中發現了0.5次延遲,但是您應該測試最慢的設備。

如果您不想使用塊,也可以使用單獨的方法來包裝區域設置代碼,並調用類似[self performSelector:@selector(nameOfMethod) withObject:nil afterDelay:0.5f];的東西。

地圖將開始加載,然後快速設置到設置的區域。如果您不喜歡此動畫,請務必將animated參數設置爲NO

+0

這對我不起作用,即使我將延遲增加到1.5秒。 –

相關問題