ios - 向 MKMapView 注释添加操作
<p><p>我有一个带有一堆注释的 MKMapView,但我无法向它们添加操作。我只是在创建一个 MKPointAnnotation,但我无法为其添加操作。每当我单击注释时,什么都没有发生。这是我设置注释的方式-</p>
<pre><code>CLLocationCoordinate2D monteVista;
monteVista.latitude = (double) 37.83029;
monteVista.longitude = (double) -121.98827;
MKPointAnnotation *monteVistaPoint = [ init];
monteVistaPoint.coordinate = monteVista;
monteVistaPoint.title = @"Monte Vista";
CLLocationCoordinate2D sanRamonValley;
sanRamonValley.latitude = (double) 37.82609;
sanRamonValley.longitude = (double) -122.00603;
MKPointAnnotation *sanRamonValleyPoint = [ init];
sanRamonValleyPoint.coordinate = sanRamonValley;
sanRamonValleyPoint.title = @"San Ramon Valley";
CLLocationCoordinate2D doughertyValley;
doughertyValley.latitude = (double) 37.76845;
doughertyValley.longitude = (double) -121.90342;
MKPointAnnotation *doughertyValleyPoint = [ init];
doughertyValleyPoint.coordinate = doughertyValley;
doughertyValleyPoint.title = @"Dougherty Valley";
NSArray *points = [ initWithObjects: monteVistaPoint, sanRamonValleyPoint, doughertyValleyPoint, nil];
;
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *annotationView = [ initWithAnnotation:annotation reuseIdentifier:nil];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
NSLog(@"Pin Created");
return annotationView;
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
MKPointAnnotation *testPoint = [ init];
testPoint = view.annotation;
self.testText.text = testPoint.title;
NSLog(@"Selected");
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您需要更改 <code>viewForAnnotation:</code> 方法以自定义,然后只需实现 <strong>openDetail: action 方法</strong></p>
<pre><code>- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if (annotation == mapView.userLocation)
{
return nil;
}
static NSString *identifier = @"CustomAnnotation";
MKAnnotationView* annView = nil;
if (]) {
annView = (MKAnnotationView *) ;
if (annView == nil) {
annView = [ initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annView.annotation = annotation;
}
UIImage *image = nil;
image = ;
UIImageView *imageView = [ initWithImage:image];
;
;
// ];
UIButton*accessory = ;
];
;
;
;
}
;
;
return annView;
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 向 MKMapView 注释添加操作,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/21594057/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/21594057/
</a>
</p>
页:
[1]