用于地图注释随机化的 iOS 自定义图标

Posted

技术标签:

【中文标题】用于地图注释随机化的 iOS 自定义图标【英文标题】:iOS custom Icons for map annotations randomizing 【发布时间】:2014-05-23 15:29:49 【问题描述】:

在我的地图视图中,我正在从 json 加载位置。 (效果很好)

在我的 json 中,我有一个名为“type”的节点,它有助于在 viewForAnnotation 中设置图像(效果很好)

我有一组单选按钮,可以删除所有注释,然后根据类型仅重新加载某些注释(效果很好 - 排序)

这个问题是当单选按钮被按下时,所有数据都是正确的,但是图标全部重新排列(看起来是随机的)。这个标题、副标题、电话和类型都仍然正确。

看起来当按钮被按下时 viewForAnotaion 没有被触发。至少没有任何东西登录该方法。但那只是设置图标的地方。我迷路了。

代码

- (void)whichPins:(int)t 

    for(id key in json) 
        id value = [json objectForKey:key];
        NSString *address = [value valueForKey:@"address"];
        NSString *latitude = [value valueForKey:@"latitude"];
        NSString *longitude = [value valueForKey:@"longitude"];

        NSArray* foo = [address componentsSeparatedByString: @":"];
        NSString* address2 = [foo objectAtIndex: 0];
        phone = [foo objectAtIndex: 1];

        double myLatitude = [latitude doubleValue];
        double myLongitude = [longitude doubleValue];

        MKCoordinateRegion location1;
        location1.center.latitude = myLatitude;
        location1.center.longitude = myLongitude;
        location1.span.longitudeDelta = 0.1;
        location1.span.latitudeDelta = 0.1;

        ann1 = [[[MapAnnotation alloc] init] autorelease];
        ann1.title = [NSString stringWithFormat:@"%@",[value valueForKey:@"title"]];
        ann1.subtitle = [NSString stringWithFormat:@"%@",address2];
        ann1.phone = [NSString stringWithFormat:@"%@",phone];
        ann1.coordinate = location1.center;
        ann1.type = [value valueForKey:@"type"];

        if (t == 0) 
            [mapView addAnnotation:ann1];
         else if (t == 1 && [ann1.type isEqualToString:@"1"]) 
            [mapView addAnnotation:ann1];
         else if (t == 2 && [ann1.type isEqualToString:@"2"]) 
            [mapView addAnnotation:ann1];
         else if (t == 3) 
            if ([ann1.type isEqualToString:@"0"] || [ann1.type isEqualToString:@"3"] || [ann1.type isEqualToString:@"4"]) 
                [mapView addAnnotation:ann1];
            
         else 

        //NSLog(@"Title: %@ , type: %@", ann1.subtitle, ann1.type);

        [phone retain];
        [json retain];
    
    //NSLog(@"/////////////////////////////////////////////////////////////////");


- (MKAnnotationView *)mapView:(MKMapView *)mapView2 viewForAnnotation:(id<MKAnnotation>)annotation 

    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;
    static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
    if(annotationView)
        return annotationView;
    else 
        MKAnnotationView *annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation
                                                                         reuseIdentifier:AnnotationIdentifier] autorelease];
        annotationView.canShowCallout = YES;
        annotationView.draggable = NO;

        if (![((MapAnnotation *)annotation).phone isEqualToString: @"0"]) 
            UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            [rightButton setImage:[UIImage imageNamed:@"map_phone_icon"] forState:UIControlStateNormal];
            [rightButton addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
            [rightButton setTitle:annotation.title forState:UIControlStateNormal];
            annotationView.rightCalloutAccessoryView = rightButton;
        
        if ([((MapAnnotation *)annotation).type isEqualToString: @"0"]) 
            annotationView.image = [UIImage imageNamed:@"popomap.png"];
         else if ([((MapAnnotation *)annotation).type isEqualToString: @"1"]) 
            annotationView.image = [UIImage imageNamed:@"robbery.png"];
         else if ([((MapAnnotation *)annotation).type isEqualToString: @"2"]) 
            annotationView.image = [UIImage imageNamed:@"other.png"];
         else if ([((MapAnnotation *)annotation).type isEqualToString: @"3"]) 
            annotationView.image = [UIImage imageNamed:@"firemap.png"];
         else if ([((MapAnnotation *)annotation).type isEqualToString: @"4"]) 
            annotationView.image = [UIImage imageNamed:@"doctormap.png"];
        


        return annotationView;
    
    return nil;


- (IBAction)onRadioBtn:(RadioButton*)sender 
    [mapView removeAnnotations:mapView.annotations];

    if ([sender.titleLabel.text isEqualToString:@"All"]) 
        rbtnType = 0;
    
    if ([sender.titleLabel.text isEqualToString:@"Crime"]) 
        rbtnType = 1;
    
    if ([sender.titleLabel.text isEqualToString:@"Other"]) 
        rbtnType = 2;
    
    if ([sender.titleLabel.text isEqualToString:@"Stations"]) 
        rbtnType = 3;
    
    [self whichPins:rbtnType];

【问题讨论】:

【参考方案1】:

annotationView 已经创建,所以当我再次加载图钉时它忽略了图标代码:

MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if(annotationView)
    return annotationView;

改成:

MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
    if(annotationView) 
        if (![((MapAnnotation *)annotation).phone isEqualToString: @"0"]) 
            UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            [rightButton setImage:[UIImage imageNamed:@"map_phone_icon"] forState:UIControlStateNormal];
            [rightButton addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
            [rightButton setTitle:annotation.title forState:UIControlStateNormal];
            annotationView.rightCalloutAccessoryView = rightButton;
        
        if ([((MapAnnotation *)annotation).type isEqualToString: @"0"]) 
            annotationView.image = [UIImage imageNamed:@"popomap.png"];
         else if ([((MapAnnotation *)annotation).type isEqualToString: @"1"]) 
            annotationView.image = [UIImage imageNamed:@"robbery.png"];
         else if ([((MapAnnotation *)annotation).type isEqualToString: @"2"]) 
            annotationView.image = [UIImage imageNamed:@"other.png"];
         else if ([((MapAnnotation *)annotation).type isEqualToString: @"3"]) 
            annotationView.image = [UIImage imageNamed:@"firemap.png"];
         else if ([((MapAnnotation *)annotation).type isEqualToString: @"4"]) 
            annotationView.image = [UIImage imageNamed:@"doctormap.png"];
        

        return annotationView;

【讨论】:

以上是关于用于地图注释随机化的 iOS 自定义图标的主要内容,如果未能解决你的问题,请参考以下文章

Flutter 多自定义图标未显示在地图视图上

android开发 百度地图3.0以上版本,如何显示自定义标记图标?

用户位置的自定义注释视图不移动地图视图

如何将自定义标记图标添加到 Google 地图?

如何在 Mapbox iOS 中的特定坐标上放置自定义注释的底部

地图注释显示不正确的自定义图钉图像