如何在地图中创建多色图钉?
我的地图需要显示“场地”的红色图钉,以及“停车场”附近和周围的蓝色图钉。
问题是两个引脚显示为相同的颜色。
代码如下:
我的 pinAnnotation 类如下所示:
#import <MapKit/MapKit.h>
@interface mapPinAnnotation : NSObject <MKAnnotation> {
NSString *title;
NSString *subtitle;
NSString *pinType; // this string will be set to either "Venue" or "Parking"
CLLocationCoordinate2D coordinate;
}
@property (nonatomic, retain) NSString *title, *subtitle;
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) NSString *pinType;
@end
这是实现:
#import "mapPinAnnotation.h"
@implementation mapPinAnnotation
@synthesize coordinate;
@synthesize pinType;
@synthesize title, subtitle;
-(id) initWithCoordinate: (CLLocationCoordinate2D) c {
coordinate = c;
return self;
}
@end
这是设置引脚的方法 - 请注意,我使用“tempPin”变量 - 全局声明 - 所以我可以将该引脚传递到“viewForAnnotation”方法 - 但我认为这就是问题所在:
-(void) dropThePin {
CLLocationCoordinate2D location = mapView.userLocation.coordinate;
location.latitude = latitude;
location.longitude = longitude;
if(pinAnnotation != nil) {
[mapView removeAnnotation:pinAnnotation];
[pinAnnotation release];
pinAnnotation = nil;
}
// Create (alloc/init) a Pin, set its Title & Subtitle, and add/place it:
pinAnnotation = [[mapPinAnnotation alloc] initWithCoordinate:location];
pinAnnotation.pinType = @"VENUE";
tempPin = pinAnnotation;
[pinAnnotation setTitle: @"Some Stadium"];
[pinAnnotation setSubtitle: @"123 Main St."];
[mapView addAnnotation:pinAnnotation];
// Set-Up of 2nd. Pin:
location.latitude = 12.34567;
location.longitude = -23.45678;
pinAnnotation.pinType = @"PARKING";
if(parkingLotPin != nil) {
[mapView removeAnnotation:parkingLotPin];
[parkingLotPin release];
parkingLotPin = nil;
}
// Create (alloc/init) a Pin, set its Title & Subtitle, and add/place it:
parkingLotPin = [[mapPinAnnotation alloc] initWithCoordinate:location];
tempPin = pinAnnotation;
[parkingLotPin setTitle: @"Another Venue"];
[parkingLotPin setSubtitle: @"789 S. Broad Street"];
[mapView addAnnotation:parkingLotPin];
[parkingLotPin release];
}
最后,这是“viewForAnnotation”方法:
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation {
MKPinAnnotationView *thePin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
if (tempPin.pinType == @"VENUE") {
thePin.pinColor = MKPinAnnotationColorGreen;
NSLog(@"pin-type = %@", tempPin.pinType);
} else {
thePin.pinColor = MKPinAnnotationColorPurple;
NSLog(@"pin-type = %@", tempPin.pinType);
}
thePin.animatesDrop=TRUE;
thePin.canShowCallout = YES;
thePin.calloutOffset = CGPointMake(-5, 5);
return thePin;
}
问题是两个引脚都显示相同的颜色。
有什么建议吗?
My map needs to display RED pins for "Venues", and BLUE pins for "Parking Lots" near and around them.
The problem is that both pins show up in the same color.
The code is below:
My pinAnnotation class looks like this:
#import <MapKit/MapKit.h>
@interface mapPinAnnotation : NSObject <MKAnnotation> {
NSString *title;
NSString *subtitle;
NSString *pinType; // this string will be set to either "Venue" or "Parking"
CLLocationCoordinate2D coordinate;
}
@property (nonatomic, retain) NSString *title, *subtitle;
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) NSString *pinType;
@end
This is the implementation:
#import "mapPinAnnotation.h"
@implementation mapPinAnnotation
@synthesize coordinate;
@synthesize pinType;
@synthesize title, subtitle;
-(id) initWithCoordinate: (CLLocationCoordinate2D) c {
coordinate = c;
return self;
}
@end
Here is the method setting the pins - note that I use a "tempPin" variable - declared globally - so I can then pass that pin into the "viewForAnnotation" method - but I think this is where the problem is:
-(void) dropThePin {
CLLocationCoordinate2D location = mapView.userLocation.coordinate;
location.latitude = latitude;
location.longitude = longitude;
if(pinAnnotation != nil) {
[mapView removeAnnotation:pinAnnotation];
[pinAnnotation release];
pinAnnotation = nil;
}
// Create (alloc/init) a Pin, set its Title & Subtitle, and add/place it:
pinAnnotation = [[mapPinAnnotation alloc] initWithCoordinate:location];
pinAnnotation.pinType = @"VENUE";
tempPin = pinAnnotation;
[pinAnnotation setTitle: @"Some Stadium"];
[pinAnnotation setSubtitle: @"123 Main St."];
[mapView addAnnotation:pinAnnotation];
// Set-Up of 2nd. Pin:
location.latitude = 12.34567;
location.longitude = -23.45678;
pinAnnotation.pinType = @"PARKING";
if(parkingLotPin != nil) {
[mapView removeAnnotation:parkingLotPin];
[parkingLotPin release];
parkingLotPin = nil;
}
// Create (alloc/init) a Pin, set its Title & Subtitle, and add/place it:
parkingLotPin = [[mapPinAnnotation alloc] initWithCoordinate:location];
tempPin = pinAnnotation;
[parkingLotPin setTitle: @"Another Venue"];
[parkingLotPin setSubtitle: @"789 S. Broad Street"];
[mapView addAnnotation:parkingLotPin];
[parkingLotPin release];
}
Finally, here is the "viewForAnnotation" method:
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation {
MKPinAnnotationView *thePin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
if (tempPin.pinType == @"VENUE") {
thePin.pinColor = MKPinAnnotationColorGreen;
NSLog(@"pin-type = %@", tempPin.pinType);
} else {
thePin.pinColor = MKPinAnnotationColorPurple;
NSLog(@"pin-type = %@", tempPin.pinType);
}
thePin.animatesDrop=TRUE;
thePin.canShowCallout = YES;
thePin.calloutOffset = CGPointMake(-5, 5);
return thePin;
}
The problem is that both pins show up with the same color.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将:更改
为:
Try changing:
to:
也看看这个
Also look at this