How do I drop a pin in center of开发者_如何学Python the map?
You first need to create a class that implements the MKAnnotation
protocol; this is effectively the "data" of the annotation (pin) and has little to do with the visual representation. You can then create an instance of this class, set its coordinate to the centre of the map, add it to your MKMapView
, and render it using an MKPinAnnotationView
instance.
// 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];
}
精彩评论