1

我想在地图上使用不同颜色的图钉,例如一些图钉应该是红色的,一些图钉应该是绿色的,一些图钉应该是紫色的。

我正在使用下面的代码,在这段代码中,一次只会丢弃一个颜色引脚。

我想知道,我们可以在地图上同时放置不同颜色的图钉吗

-(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];
}
4

2 回答 2

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;
}
于 2013-03-30T09:03:41.483 回答
0

将以下代码放在 if 语句之外。否则当pin被重用时,下面的代码将不会被调用。

    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];
    }
于 2013-03-06T07:30:27.683 回答