1
我有這個自定義MKPinAnnotation到我需要添加圖像(如縮略圖)。我應該也可以通過點擊它全屏打開圖像。什麼是最好的方式來做到這一點?將縮略圖添加到MKPinAnnotation
我有這個自定義MKPinAnnotation到我需要添加圖像(如縮略圖)。我應該也可以通過點擊它全屏打開圖像。什麼是最好的方式來做到這一點?將縮略圖添加到MKPinAnnotation
一些想法。
如果您不想在地圖上一針,而是一些自定義圖像,您可以設置地圖的委託,然後寫一個viewForAnnotation
,做一樣的東西:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[CustomAnnotation class]])
{
static NSString * const identifier = @"MyCustomAnnotation";
// if you need to access your custom properties to your custom annotation, create a reference of the appropriate type:
CustomAnnotation *customAnnotation = annotation;
// try to dequeue an annotationView
MKAnnotationView* annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView)
{
// if we found one, update its annotation appropriately
annotationView.annotation = annotation;
}
else
{
// otherwise, let's create one
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:identifier];
annotationView.image = [UIImage imageNamed:@"myimage"];
// if you want a callout with a "disclosure" button on it
annotationView.canShowCallout = YES;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
// If you want, if you're using QuartzCore.framework, you can add
// visual flourishes to your annotation view:
//
// [annotationView.layer setShadowColor:[UIColor blackColor].CGColor];
// [annotationView.layer setShadowOpacity:1.0f];
// [annotationView.layer setShadowRadius:5.0f];
// [annotationView.layer setShadowOffset:CGSizeMake(0, 0)];
// [annotationView setBackgroundColor:[UIColor whiteColor]];
}
return annotationView;
}
return nil;
}
如果您提供標準標註做到這一點(如上圖所示),然後你可以告訴地圖你要當用戶點擊上標註的信息披露按鈕做什麼:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
if (![view.annotation isKindOfClass:[CustomAnnotation class]])
return;
// do whatever you want to do to go to your next view
}
如果你真的想繞過其披露按鈕標註,而是直接去另一個視圖控制器,當你在註釋視圖標籤,你會:
在viewForAnnotation
設置canShowCallout
到NO
;和
欲瞭解更多信息,請參閱Annotating Maps在位置感知編程指南。