Question

I want to different color pins on the map for example some pins should be red,some pins should be green and some pins should be purple.

I am using the below code, in this code at one time only one color pins will be dropped.

I want to know, can we drop different color pins same time in the Map

-(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];
}
Was it helpful?

Solution

I am also using different color pins on Map. I am using the below code. I am able to see 3 different color pins. I am working on iOS6, so my suggestion is test this code on 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;
}

OTHER TIPS

Place the following code outside the if statement. Otherwise when the pin is reused, the following code will not be called.

    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];
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top