gpt4 book ai didi

ios - 更改不同位置的 map 图钉颜色

转载 作者:行者123 更新时间:2023-11-29 02:42:08 25 4
gpt4 key购买 nike

我已经四处寻找了很久,但我还没有真正找到一个好的答案。我正在尝试更改几个不同位置的图钉颜色(1 个红色,1 个紫色......)

我有一个单独的注释类 pinPlaceMark.h #进口 #导入

@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 的另一个实例并根据坐标返回它,但它对我不起作用。

感谢您提供的任何帮助。

最佳答案

这是我建议的一个例子......

首先,将引脚颜色属性添加到您的自定义注释类 PinPlaceMark,这样您就可以单独设置每个引脚的颜色。此属性应该在实现 MKAnnotation(模型对象)的类中。该属性不必是“pin color”——只是一些特定于每个注释的值,这样可以很容易地决定在 viewForAnnotation 中将 View 设置为什么 pin 颜色:

@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 委托(delegate)方法以检查注释是否属于 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;
}

关于ios - 更改不同位置的 map 图钉颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25629858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com