我想在MKMapview中添加一個UIImage。如何在iPhone中的MKMapView中添加UIImage?
即:一個特定的位置,我想添加一個名爲「apple.jpg」的圖像。
我不知道該怎麼做。
因爲我已經添加了一個可拖動的引腳作爲註釋。但我不知道我是否可以添加多個圖像。
我想在MKMapview中添加一個UIImage。如何在iPhone中的MKMapView中添加UIImage?
即:一個特定的位置,我想添加一個名爲「apple.jpg」的圖像。
我不知道該怎麼做。
因爲我已經添加了一個可拖動的引腳作爲註釋。但我不知道我是否可以添加多個圖像。
對於添加圖像的MKMapView的部分請參見本教程...
和添加圖片作爲引腳上的MapView然後使用該婁代碼...
MKAnnotationView *annotationView = [[[MKAnnotationView alloc] init];
annotationView.image = [UIImage imageNamed:@"apple.png"];
annotationView.annotation = annotation;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annotationView.rightCalloutAccessoryView.tag = 101;
annotationView.canShowCallout = YES;
[yourMapView addAnnotation:annotationView];
您的圖片必須爲b e png格式請參閱下面的代碼。
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pinView"];
if (!pinView) {
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pinView"] autorelease];
pinView.image = [UIImage imageNamed:@"apple.png"];
pinView.canShowCallout = YES;
}
}
這是工作代碼,我已經與下面的代碼沒有練琴圖像png格式,.jpg或.gif的任何甲做,你可以用它
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *pinAnnotation = nil;
NSString *defaultPinID = @"myPin";
pinAnnotation = (MKPinAnnotationView *)[mapview dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if (pinAnnotation == nil)
pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinAnnotation.image = [UIImage imageNamed:@"marker_postoffice.png"];
pinAnnotation.annotation = annotation;
pinAnnotation.canShowCallout = YES;
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[infoButton addTarget:self action:@selector(showDetails)forControlEvents:UIControlEventTouchUpInside];
pinAnnotation.rightCalloutAccessoryView = infoButton;
// now you can set diff image by [annotation title] you can set diff image with if else condition like below code
if([[annotation title] isEqualToString:objAppDelegate.OfficePinTitle])
{
pinAnnotation.image = [UIImage imageNamed:@"marker_postoffice.png"];
pinAnnotation.annotation = annotation;
pinAnnotation.canShowCallout = YES;
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
infoButton addTarget:self
action:@selector(showDetails)forControlEvents:UIControlEventTouchUpInside];
pinAnnotation.rightCalloutAccessoryView = infoButton;
}
else
{
pinAnnotation.image = [UIImage imageNamed:@"marker_red_postoffice.png"];
pinAnnotation.canShowCallout = YES;
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[infoButton addTarget:self action:@selector(showDetails)forControlEvents:UIControlEventTouchUpInside];
pinAnnotation.rightCalloutAccessoryView = infoButton;
}
}
我猜你是問如何做一個自定義的MKAnnotationView,是這樣嗎? – Kassem
不,我想簡單地添加一個圖像而不是註釋。即我想在給定的位置添加蘋果的圖片。 – Hemang
MapKit中的任何地理定位事物都是一個Annotation,它的可視化表示形式是MKAnnotationView – whiteagle