【问题标题】:iOS: Can we drop different color pins same time on MapiOS:我们可以在地图上同时放置不同颜色的图钉吗
【发布时间】:2013-03-30 17:03:41
【问题描述】:

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

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

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

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

【问题讨论】:

  • 在哪里定义m_pinColor,它是什么类型的变量?

标签: ios objective-c google-maps mkannotation pins


【解决方案1】:

我还在地图上使用不同颜色的图钉。我正在使用下面的代码。我能看到 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;
}

【讨论】:

    【解决方案2】:

    将以下代码放在 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];
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多