2011-04-29 67 views

回答

4

您首先需要創建一個實現MKAnnotation協議的類;這實際上是註釋(pin)的「數據」,並且與視覺表示無關。然後,您可以創建此類的實例,將其座標設置爲地圖中心,將其添加到您的MKMapView,並使用MKPinAnnotationView實例渲染它。

// In your view controller, assuming you've created the "MyAnnotation" class. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 

    MyAnnotation *annotation = [[[MyAnnotation alloc] init] autorelease]; 
    annotation.coordinate:self.mapView.centerCoordinate; 
    [self.mapView addAnnotation:annotation]; 
} 

- (MKAnnotationView *)mapView:(MKMapView *)mapView 
    viewForAnnotation:(id <MKAnnotation>)annotation { 
    // Remember to reuse annotation views by calling MKMapView's 
    // dequeueReusableAnnotationViewWithIdentifier:! 
    return [[[MKPinAnnotationView alloc] initWithAnnotation:annotation 
     reuseIdentifier:@"string"] autorelease]; 
}