我试图在Xcode(objetive-c)上的每个地图引脚的leftCalloutAccessoryView中获取不同的图像。我在互联网上寻找其他回复,但我没有找到这个(只有如何更改图像)。但我想更改引脚标题左侧引脚内的图像。有人能帮我吗?非常感谢你。
这是我的RestMapPin.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface RestMapPin : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, copy) NSString *image;
@end
这是我的RestMapPin.m:
#import "RestMapPin.h"
@implementation RestMapPin
@synthesize coordinate, title, subtitle, image;
@end
这是我的MapViewController.h:
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface MapViewController : UIViewController {
MKMapView *mapView;
}
@property (weak, nonatomic) IBOutlet UIBarButtonItem *barButton;
@property (nonatomic, retain) IBOutlet MKMapView *mapView;
@property(nonatomic, retain) CLLocationManager *locationManager;
@end
这是我的MapViewController.m:
#import "MapViewController.h"
#import "SWRevealViewController.h"
#import "RestMapPin.h"
#import "RestViewController.h"
@interface MapViewController ()
@end
@implementation MapViewController
@synthesize mapView;
- (void)viewDidLoad {
[super viewDidLoad];
_barButton.target = self.revealViewController;
_barButton.action = @selector(revealToggle:);
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
/*Babelia*/
MKCoordinateRegion region_Babelia = { {0.0, 0.0}, {0.0, 0.0}};
region_Babelia.center.latitude = 40.4234778;
region_Babelia.center.longitude = -3.686283000000003;
region_Babelia.span.longitudeDelta = 0.1f;
region_Babelia.span.latitudeDelta = 0.1f;
[mapView setRegion:region_Babelia animated:YES];
RestMapPin *ann_Babelia = [[RestMapPin alloc] init];
ann_Babelia.title = @"Babelia";
ann_Babelia.subtitle = @"Barrio de Salamanca";
ann_Babelia.coordinate = region_Babelia.center;
[mapView addAnnotation:ann_Babelia];
/*Bacira*/
MKCoordinateRegion region_Bacira = { {0.0, 0.0}, {0.0, 0.0}};
region_Bacira.center.latitude = 40.43375390000001;
region_Bacira.center.longitude = -3.699036299999989;
region_Bacira.span.longitudeDelta = 0.1f;
region_Bacira.span.latitudeDelta = 0.1f;
[mapView setRegion:region_Bacira animated:YES];
RestMapPin *ann_Bacira = [[RestMapPin alloc] init];
ann_Bacira.title = @"Bacira";
ann_Bacira.subtitle = @"Chamberí";
ann_Bacira.coordinate = region_Bacira.center;
[mapView addAnnotation:ann_Bacira];
}
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *pinView = nil;
if(annotation != mapView.userLocation) {
static NSString *defaultPinID = @"com.invasivecode.pin";
pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID];
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:@"pin2@2x.png"];
}
else {
//[mapView.userLocation setTitle:@"I am here"];
}
UIImageView *leftIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pin2@2x.png"]];
pinView.leftCalloutAccessoryView = leftIconView;
leftIconView.layer.cornerRadius = 6;
leftIconView.clipsToBounds = YES;
leftIconView.layer.borderWidth = 1;
return pinView;
}
@end
我知道这与viewForAnnotation方法中的代码类似:
UIImageView *leftIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pin2@2x.png"]];
pinView.leftCalloutAccessoryView = leftIconView;
但是我不知道如何改变每个地图引脚的不同之处(&#34; Babelia&#34;&#34; Bacira&#34;)。 非常感谢您的回复!
答案 0 :(得分:1)
@interface RestMapPin : NSObject <MKAnnotation>
{
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
NSString *pincolor;
}
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, copy) NSString *pincolor;
@property (nonatomic, copy) NSString *image;
给出引脚值
ann_Bacira. pincolor = @“red”;
ann_Babelia.pincolor = “green”;
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *pinView = nil;
UIImage *objImage = nil;
if(annotation != mapView.userLocation)
{
static NSString *defaultPinID = @"com.invasivecode.pin";
pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID];
pinView.canShowCallout = YES;
if([annotation.pincolor isEqualToString:@"green"])
{
objImage =[UIImage imageNamed:@“image1@2x.png"]
}
else if([annotation.pincolor isEqualToString:@"red"])
{
objImage =[UIImage imageNamed:@“image2@2x.png"]
}
pinView.image = objImage;
}
else {
//[mapView.userLocation setTitle:@"I am here"];
}
return pinView;
}