2010-06-05 13 views

回答

0

首先你要創建MKAnnotation協議類(這裏MyAnnotation類實現MKAnnotation協議)。這個類應該包括lat,long,title,subtitle以及任何你想要的。

其次,在您的視圖控制器,你要顯示的腳,你會實現這個代碼,

AnnObj = [[MyAnnotation alloc] init]; 

AnnObj.latitude = [[latitude objectAtIndex:storyIndex] floatValue]; 

AnnObj.longitude = [[longitude objectAtIndex:storyIndex] floatValue]; 

MKCoordinateRegion region; 

region.center = location; 

MKCoordinateSpan span; 

region.center.latitude = [[latitude objectAtIndex:storyIndex] floatValue]; 

region.center.longitude = [[longitude objectAtIndex:storyIndex] floatValue]; 

span.latitudeDelta = 0.0005; 

span.longitudeDelta = 0.0005; 

region.span = span; 

[mapview setRegion:region animated:TRUE]; 

AnnObj.title = titleString; 

AnnObj.subTitle = subTitleString; 

NSString * titleString = [[buildingNames objectAtIndex:storyIndex] stringByReplacingOccurrencesOfString:@"\n\t" withString:@""]; 

[eventPoints addObject:AnnObj]; 

[mapview addAnnotations:eventPoints]; 

Third, Implement the MKAnnotation delegate method, 

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

    if (lmapView.userLocation == annotation){ 

     return nil; 
     } 

    MKAnnotationView* myCusAnn = (MKAnnotationView*)[lmapView dequeueReusableAnnotationViewWithIdentifier:@"eventview"]; 

    if(myCusAnn == nil) 
    { 
     myCusAnn = [[[ MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"eventview"] autorelease]; 

    } 

    myCusAnn.canShowCallout = YES; 

    [myCusAnn setEnabled:YES]; 

    return myCusAnn; 

    } 

我希望這會幫助你。

1
  1. 創建實現MKAnnotation協議的新類。這將保存緯度/經度,並且還會有一個標題和描述,如果您在地圖上渲染註釋後選擇註釋,將會顯示該標題和描述。
  2. 顯示地圖的視圖控制器需要實現MKMapViewDelegate協議的 - - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation;方法。這種方法中的代碼看起來像底部的代碼(對於格式不好的道歉,我無法在這裏或底部得到正確的答案)。

  3. 然後在你的控制器代碼某些時候,你需要調用沿[self.mapView addAnnotation: annotation];線,其中註釋是在步驟1中創建自定義的註釋類的實例的東西,與緯度/經度設置等

  4. 最後,爲了讓viewForAnnotation方法得到正確調用,並且在界面構建器中很容易丟失,請確保您將MKMapView的委託出口設置爲您的控制器(實現MKMapViewDelegate協議)

    -(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation { 
    
        static NSString *AnnotationViewIdentifier = @"annotationViewIdentifier"; 
    
        MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier: AnnotationViewIdentifier]; 
    
        if (annotationView == nil) { 
        annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier: AnnotationViewIdentifier] autorelease]; 
    
    // This is all optional and depends on your requirements 
        annotationView.animatesDrop = NO; 
        annotationView.canShowCallout = YES; 
        annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
        annotationView.enabled = YES; 
        annotationView.pinColor = MKPinAnnotationColorGreen; 
        } 
        return annotationView; 
    } 
    
+0

感謝您的回答.. – 2010-06-07 05:55:11

+0

也許您可以編輯您的答案,以便更好地突出顯示代碼。 – testing 2010-10-05 15:58:43

相關問題