2013-07-25 32 views
1

我想補充一個觀點,當我在一個針點擊,但只顯示標題和副標題,不,我沒有補充的觀點。創建自定義調出查看

這裏是我的代碼

-(void) inserirMapa { 
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate]; 
    NSMutableArray *pins = [[NSMutableArray alloc]init]; //array de Annotations 
    for(int iCnt = 0; iCnt < [_listaPins count]; iCnt++) { 
     Pin *pin = (Pin *) [_listaPins objectAtIndex:iCnt]; 

     CLLocationCoordinate2D start; 
     start.latitude = pin.place.latitude; 
     start.longitude = pin.place.longitude; 
     MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(start, 800, 800); 
     [_myMapView setRegion:[_myMapView regionThatFits:region] animated:YES]; 

     // Add an annotation 
     MKPointAnnotation *point = [[MKPointAnnotation alloc] init]; 
     point.coordinate = start; 
     point.title = @"Where am I?"; 
     point.subtitle = @"I'm here!!!"; 

     [_myMapView addAnnotation:point]; 

     [pins addObject:point];//adiciona a annotation no array 
     [point release]; 
    } 
    self.myAnnotations= [NSArray arrayWithArray:pins]; //diz que o array myAnnotations é igual ao array estacionamentos(que contem as annotations 
     [pins release]; 
     [self configurarZoomDoMapaComLatitude:appDelegate.latitude eLongitude:appDelegate.longitude]; //configura o Zoom inicial do mapa 


    [_myMapView addAnnotations:_myAnnotations]; 

    [_viewLoading setHidden:YES]; 
} 

這裏我添加自定義視圖。我把一個斷點在這裏和這個函數被調用,但是當我在針點擊,appearas只有標題和subtitle..ignoring我添加

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    MKAnnotationView* annotationView = nil; 

    //your code 

    UIView *vw = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
    vw.backgroundColor = [UIColor redColor]; 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 50, 50)]; 
    label.numberOfLines = 4; 
    label.text = @"hello\nhow are you\nfine"; 
    [vw addSubview:label]; 
    annotationView.leftCalloutAccessoryView = vw; 
    return annotationView; 

} 

回答

6

viewForAnnotation認爲,代碼聲明並初始化annotationViewnil並且永遠不會實例化它(alloc + init)。

因此,委託方法返回nil它告訴地圖視圖爲「創建此註解默認視圖」。除用戶位置以外的註釋的默認視圖是帶有僅顯示標題和副標題的標註的紅色別針。


但是,如果你只是頁頭+初始化一個普通MKAnnotationView修復它,註釋是不可見的,因爲默認爲MKAnnotationView沒有內容。您需要設置其image或添加一些子視圖。

相反,你可以ALLOC +初始化的MKPinAnnotationView在默認情況下會顯示紅色的大頭針。
你也應該實現的註解視圖重新使用通過調用dequeueReusableAnnotationViewWithIdentifier以提高性能時,有很多註解:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    static NSString *reuseId = @"ann"; 

    MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView 
     dequeueReusableAnnotationViewWithIdentifier:reuseId]; 
    if (annotationView == nil) 
    { 
     annotationView = [[MKPinAnnotationView alloc] 
           initWithAnnotation:annotation 
            reuseIdentifier:@"test"]; 
     annotationView.canShowCallout = YES; //set to YES, default is NO 

     //your code 

     UIView *vw = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
     vw.backgroundColor = [UIColor redColor]; 
     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 50, 50)]; 
     label.numberOfLines = 4; 
     label.text = @"hello\nhow are you\nfine"; 
     [vw addSubview:label]; 
     annotationView.leftCalloutAccessoryView = vw; 
    } 
    else 
    { 
     //Update view's annotation reference 
     //because we are re-using view that may have 
     //been previously used for another annotation... 
     annotationView.annotation = annotation; 
    } 

    return annotationView; 
} 


這應該可以解決leftCalloutAccessoryView沒有出現自定義。
但是,請注意什麼文件說,爲leftCalloutAccessoryView(和rightCalloutAccessoryView):

你的視線的高度應爲32個像素或更低。

代碼創建的自定義視圖和標籤都高於32像素。


有關,增加了註釋的循環,不相關的評論:
你並不需要調用setRegion爲要添加每個註釋。
調用setRegion只是告訴地圖視圖以顯示指示區域。
是不需要添加註釋(地圖上沒有必須顯示你添加的註釋座標)。

+0

Thx,它的工作原理。但是,我如何創建更大的視圖?點擊圖釘時,我需要顯示帶有ImageViews,標籤和按鈕的100 x100視圖。這是可能的? – shadsauhduisad

+0

我也發現,使用自定義註釋視圖這個[示例代碼(http://blog.asolutions.com/2010/09/building-custom-map-annotation-callouts-part-1/)的示例代碼。 – shadsauhduisad

+0

正如你所看到的,內置的標註視圖並不是非常可定製的。唯一的方法是禁用它(canShowCallout = NO),並在didSelectAnnotationView中,將自定義標註視圖添加到註釋視圖本身(這是一種方法)。您找到的示例代碼似乎是一個受歡迎的選項。 – Anna