我想在地图上使用不同的颜色引脚,例如某些引脚应为红色,某些引脚应为绿色,某些引脚应为紫色。
我使用下面的代码,在这段代码中,只有一个颜色引脚会被丢弃。
我想知道,我们可以在地图中同时删除不同颜色的针脚
-(void)showMap
{
[map_View setZoomEnabled:YES];
[map_View setScrollEnabled:YES];
CLLocationCoordinate2D coordinate;
coordinate.latitude = 49.2802;
coordinate.longitude = -123.1182;
map_View.region = MKCoordinateRegionMakeWithDistance(coordinate, 2000, 2000);
// Set 10 random locations on the map for testing purposes
//
for(int i=0; i<10; i++) {
CGFloat latDelta = rand()*.035/RAND_MAX -.02;
CGFloat longDelta = rand()*.03/RAND_MAX -.015;
CLLocationCoordinate2D newCoord = { coordinate.latitude + latDelta, coordinate.longitude + longDelta };
RetailerAnnotation *ann = [[RetailerAnnotation alloc] initWithLocation:newCoord];
// ann.coordinate = newCoord;
//m_pinColor = @"BLUE";
if(i< 4)
{
m_pinColor = @"RED";
}
else if(i>=4 && i<7)
{
m_pinColor = @"BLUE";
}
else if(i>=7 && i<10)
{
m_pinColor = @"GREEN";
}
NSLog(@"pin color:%@",m_pinColor);
[ann setTitle:[NSString stringWithFormat:@"Title%d",i]];
[ann setSubtitle:[NSString stringWithFormat:@"subTitle%d",i]];
[map_View addAnnotation:ann];
[ann release];
}
}
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
static NSString *identifier = @"myPin";
MKPinAnnotationView *pinView = nil;
NSLog(@"pin color0:%@",m_pinColor);
pinView = (MKPinAnnotationView *)[map_View dequeueReusableAnnotationViewWithIdentifier:identifier];
if (pinView == nil)
{
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
/*
if([m_pinColor isEqualToString:@"RED"]) {
NSLog(@"pin color1:%@",m_pinColor);
[pinView setPinColor:MKPinAnnotationColorPurple];
}
else if([m_pinColor isEqualToString:@"GREEN"]){
NSLog(@"pin color2:%@",m_pinColor);
[pinView setPinColor:MKPinAnnotationColorGreen];
}
else if([m_pinColor isEqualToString:@"BLUE"]){
NSLog(@"pin color3:%@",m_pinColor);
[pinView setPinColor:MKPinAnnotationColorRed];
}*/
}
if([m_pinColor isEqualToString:@"RED"]) {
NSLog(@"pin color1:%@",m_pinColor);
[pinView setPinColor:MKPinAnnotationColorRed];
}
else if([m_pinColor isEqualToString:@"GREEN"]){
NSLog(@"pin color2:%@",m_pinColor);
[pinView setPinColor:MKPinAnnotationColorGreen];
}
else if([m_pinColor isEqualToString:@"BLUE"]){
NSLog(@"pin color3:%@",m_pinColor);
[pinView setPinColor:MKPinAnnotationColorPurple];
}
return pinView;
//[pinView release];
}
答案 0 :(得分:3)
我也在地图上使用不同颜色的针脚。我使用以下代码。我能看到3种不同颜色的针脚。我正在iOS6上工作,所以我的建议是在iOS6上测试这段代码。
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{ static NSString *identifier = @"myPin";
MKPinAnnotationView *pinView = nil;
pinView = (MKPinAnnotationView *)[map_View dequeueReusableAnnotationViewWithIdentifier:identifier];
if (pinView == nil)
{
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
}
if([m_pinColor isEqualToString:@"Red"]) {
[pinView setPinColor:MKPinAnnotationColorRed];
}
else if([m_pinColor isEqualToString:@"Green"]){
[pinView setPinColor:MKPinAnnotationColorGreen];
}
else if([m_pinColor isEqualToString:@"Purple"]){
[pinView setPinColor:MKPinAnnotationColorPurple];
}
return pinView;
}
答案 1 :(得分:0)
将以下代码放在if语句之外。否则,当重用引脚时,将不会调用以下代码。
if([m_pinColor isEqualToString:@"RED"]) {
NSLog(@"pin color1:%@",m_pinColor);
[pinView setPinColor:MKPinAnnotationColorPurple];
}
else if([m_pinColor isEqualToString:@"GREEN"]){
NSLog(@"pin color2:%@",m_pinColor);
[pinView setPinColor:MKPinAnnotationColorGreen];
}
else if([m_pinColor isEqualToString:@"BLUE"]){
NSLog(@"pin color3:%@",m_pinColor);
[pinView setPinColor:MKPinAnnotationColorRed];
}