我一直在四处寻找,但我还没有找到一个好的答案。 我尝试在几个不同的位置更改引脚的颜色(1个红色,1个紫色......)
我有一个单独的注释类 pinPlaceMark.h #进口 #import
@interface PinPlaceMark : NSObject <MKAnnotation>
@property(nonatomic, readwrite) CLLocationCoordinate2D coordinate;
@property(nonatomic, strong) NSString *myTitle;
@property(nonatomic, strong) NSString *mySubTitle;
- (id) initWithCoordinate: (CLLocationCoordinate2D) coordinate;
@end
pinPlaceMark.m
#import "PinPlaceMark.h"
@implementation PinPlaceMark
- (id) initWithCoordinate:(CLLocationCoordinate2D)coordinate
{
self = [super init];
if (self)
{
self.coordinate = coordinate;
}
return self;
}
- (NSString *) subtitle
{
return self.mySubTitle;
}
- (NSString *) title
{
return self.myTitle;
}
@end
mapViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "PinPlaceMark.h"
@interface MapViewController : UIViewController <MKMapViewDelegate>
@property (nonatomic, strong) IBOutlet MKMapView *mapView;
mapViewController.m
- (void) addPinWithCoordinate: (CLLocationCoordinate2D) pinLocation
{
PinPlaceMark *placeMark = [[PinPlaceMark alloc] initWithCoordinate:pinLocation];
placeMark.myTitle = @"This is my title";
placeMark.mySubTitle = @"This is my subtitle";
[self.mapView addAnnotation:placeMark];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView.delegate = self;
CLLocationCoordinate2D pinLocation1;
pinLocation1.latitude = 40.7101843;
pinLocation1.longitude = -74.0061474;
[self addPinWithCoordinate:pinLocation1];
}
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"id"];
pinView.pinColor = MKPinAnnotationColorPurple;
return pinView;
仅限一种颜色。 如何更改不同位置的颜色 我已经尝试创建另一个MKPinAnnotationView实例并根据坐标返回它,但它不适合我。
感谢您提供的任何帮助。
答案 0 :(得分:1)
这是我建议的一个例子......
首先,将pin颜色属性添加到自定义注记类PinPlaceMark
,以便您可以单独设置每个引脚的颜色。此属性应位于实现MKAnnotation
的类(模型对象)中。该物业不一定是&#34; pin color&#34; - 只是某些特定于每个注释的值,可以很容易地确定在viewForAnnotation
中设置视图的引脚颜色:
@property (nonatomic, assign) MKPinAnnotationColor myPinColor;
接下来,更新您的addPinWithCoordinate:
方法(因为您正在创建PinPlaceMark
的实例)以接受引脚颜色并进行设置:
- (void) addPinWithCoordinate:(CLLocationCoordinate2D)pinLocation
color:(MKPinAnnotationColor)pinColor //<-- new
{
PinPlaceMark *placeMark = [[PinPlaceMark alloc] initWithCoordinate:pinLocation];
placeMark.myTitle = @"This is my title";
placeMark.mySubTitle = @"This is my subtitle";
placeMark.myPinColor = pinColor; //<-- new
[self.mapView addAnnotation:placeMark];
}
接下来,更新调用addPinWithCoordinate:
的代码。示例还显示了添加了不同颜色的多个引脚:
CLLocationCoordinate2D pinLocation1;
pinLocation1.latitude = 40.7101843;
pinLocation1.longitude = -74.0061474;
[self addPinWithCoordinate:pinLocation1 color:MKPinAnnotationColorGreen];
CLLocationCoordinate2D pinLocation2 = CLLocationCoordinate2DMake(41, -75);
[self addPinWithCoordinate:pinLocation2 color:MKPinAnnotationColorRed];
CLLocationCoordinate2D pinLocation3 = CLLocationCoordinate2DMake(32, -80);
[self addPinWithCoordinate:pinLocation3 color:MKPinAnnotationColorPurple];
CLLocationCoordinate2D pinLocation4 = CLLocationCoordinate2DMake(50, -90);
[self addPinWithCoordinate:pinLocation4 color:MKPinAnnotationColorGreen];
CLLocationCoordinate2D pinLocation5 = CLLocationCoordinate2DMake(40, -120);
[self addPinWithCoordinate:pinLocation5 color:MKPinAnnotationColorRed];
CLLocationCoordinate2D pinLocation6 = CLLocationCoordinate2DMake(45, -100);
[self addPinWithCoordinate:pinLocation6 color:MKPinAnnotationColorPurple];
最后,更新viewForAnnotation
委托方法以检查注释是否为PinPlaceMark
类型并使用其myPinColor
属性:
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if (! [annotation isKindOfClass:[PinPlaceMark class]]) {
//if annotation is not a PinPlaceMark (eg. user location),
//return nil so map view draws default view (eg. blue dot) for it...
return nil;
}
static NSString *reuseId = @"id";
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (pinView == nil) {
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
pinView.canShowCallout = YES;
}
else {
pinView.annotation = annotation;
}
//Set annotation-specific properties **AFTER**
//view is dequeued or created...
PinPlaceMark *ppm = (PinPlaceMark *)annotation;
pinView.pinColor = ppm.myPinColor;
return pinView;
}
答案 1 :(得分:0)
要实现此目的,您可以生成随机彩色图像并将其分配给MKPinAnnotationView。虽然我没有在我的MAC上尝试过,但它应该可行。
以下是相同的源代码:
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"id"];
// Code for random color
CGFloat hue = ( arc4random() % 256 / 256.0 ); // 0.0 to 1.0
CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from white
CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from black
UIColor *color = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
UIImage * image = [self imageWithColor:color];
UIImageView *imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
[pinView addSubview:imageView];
return pinView;
}
- (UIImage *)imageWithColor:(UIColor *)color{
CGRect rect = CGRectMake(0.0f, 0.0f, 10.0f, 10.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
答案 2 :(得分:0)
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "MyAnnotation.h"
@interface MainViewController : UIViewController<MKMapViewDelegate>
{
NSMutableArray *annotationArray;
int i;
}
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@end
@implementation MainViewController
@synthesize mapView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.title=@"Machine Test";
mapView.delegate=self;
i=1; //use for different colors of pins
annotationArray = [[NSMutableArray alloc] init];
// Do any additional setup after loading the view from its nib.
//Annotation object1
MyAnnotation *ann1 = [MyAnnotation new];
ann1.title=@"Pune";
ann1.subtitle=@"India";
CLLocationCoordinate2D Cordinate1;
Cordinate1.latitude = 18.;
Cordinate1.longitude = 73.5;
ann1.coordinate = Cordinate1;
[annotationArray addObject:ann1];
//Annotation object2
MyAnnotation *ann2=[MyAnnotation new];
ann2.title=@"Delhi";
ann2.subtitle=@"India";
CLLocationCoordinate2D Coordinate2;
Coordinate2.latitude=25;
Coordinate2.longitude=78;
ann2.coordinate=Coordinate2;
[annotationArray addObject:ann2];
//Annotation object3
MyAnnotation *ann3=[MyAnnotation new];
ann3.title=@"Hyderabad";
ann3.subtitle=@"India";
CLLocationCoordinate2D Coordinate3;
Coordinate3.latitude=18.5;
Coordinate3.longitude=78;
ann3.coordinate=Coordinate3;
[annotationArray addObject:ann3];
//Annotation Object4
MyAnnotation *ann4=[MyAnnotation new];
ann4.title=@"Himalaya";
ann4.subtitle=@"India";
CLLocationCoordinate2D Coordinate4;
Coordinate4.latitude=28;
Coordinate4.longitude=80;
ann4.coordinate=Coordinate4;
[annotationArray addObject:ann4];
//Add all annotation objects in 1 array and pass for adding annotation
[mapView addAnnotations:annotationArray];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *AnnotationIdentifier = @"AnnotationIdentifier";
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
if (i==1) {
pinView.pinColor = MKPinAnnotationColorPurple;
i++;
}else if (i%2) {
pinView.pinColor = MKPinAnnotationColorGreen;
i++;
}else{
pinView.pinColor = MKPinAnnotationColorRed;
i++;
}
return pinView;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end;
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MyAnnotation : NSObject<MKAnnotation>
@property (copy, nonatomic)NSString *title;
@property (copy, nonatomic)NSString *subtitle;
@property (assign, nonatomic)CLLocationCoordinate2D coordinate;
@end
objMain=[[MainViewController alloc]initWithNibName:@"MainViewController" bundle:nil];
objNav=[[UINavigationController alloc]initWithRootViewController:objMain];
[self.window addSubview:objNav.view];