簡體   English   中英

MKMapview注釋縮放后動態圖釘圖像更改

[英]MKMapview annotation dynamic pin image changes after zooming

我正在做一個小項目,在地圖上顯示7種不同類型的注釋。 我的注釋來自數組中的url結果,我使用JSON對其進行了解析。 我有很多注釋,一旦地圖加載,一切看起來都不錯。 放大和縮小后,針腳圖像由於某種原因變為錯誤的針腳圖像(特定圖像,不知道為什么)。

我確定我在這里缺少什么...可以請您幫忙:)嗎?

這是我的代碼的一部分,如果您需要它,請告訴我:

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

static NSString *identifier;

if(_mapView.tag==1){identifier = @"TurbulencePin";}
if(_mapView.tag==2){identifier = @"IcingPin";}
if(_mapView.tag==3){identifier = @"WindsPin";}
if(_mapView.tag==4){identifier = @"TemperaturePin";}
if(_mapView.tag==5){identifier = @"CloudsPin";}
if(_mapView.tag==6){identifier = @"VisibilityPin";}
if(_mapView.tag==7){identifier = @"MultiplePin";}


if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;

if ([annotation isKindOfClass:[Annotation class]]) {

    CustomAnnotationView* annotationView = (CustomAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    annotationView = nil;

    if (annotationView == nil) {

        annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        annotationView.enabled = YES;
        annotationView.canShowCallout = YES;


        UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png",identifier]];
        annotationView.image = img;

    }
else
    {

        annotationView.annotation = annotation;

    }


    return annotationView;

}
return nil;

}

更新:

根據其他人的反饋,我將圖像設置的代碼修改如下:

 Annotation *myCustomAnn = (Annotation *)annotation;
 NSString *imgName = myCustomAnn.imageName;
 UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"%@Pin.png",imgName]];
 annotationView.image = img;

 return annotationView;

另外,我刪除了annotationView = nil;

但是,我無法將注釋.m中的圖像名稱設置為硬編碼值,因為我需要為每個注釋顯示不同的圖釘圖像。 我敢肯定有一個解釋,但是從mapView:viewForAnnotation:下的commentment.m可以得到的唯一值是注釋坐標( myCustomAnn.coordinate.latitudemyCustomAnn.coordinate.longitude) ,我不知道如何從annotation.m獲取其他屬性

其他屬性(例如title,imgname等)返回為null

主要問題是viewForAnnotation中的代碼依賴於外部變量_mapView.tag來確定注釋視圖。

假設地圖將在何時以及以何種頻率viewForAnnotation委托方法是不安全的。

如果注釋的視圖取決於某些值,通常最好將這些值直接嵌入到注釋對象本身中。 這樣,在viewForAnnotation委托方法中,您可以通過傳遞給方法的annotation參數來引用那些特定於annotation值。 在創建注釋時(在調用addAnnotation之前)應設置那些特定於注釋的值。

有關更多詳細信息和示例,請參見:

一個單獨的問題是代碼在調用dequeueReusableAnnotationViewWithIdentifier來取消出dequeueReusableAnnotationViewWithIdentifier后,將annotationView dequeueReusableAnnotationViewWithIdentifier設置為nil


至少在viewForAnnotation ,處理Annotation類的更正后的代碼可能看起來像這樣(不是整個方法,而是第二個if的部分):

static NSString *identifier = @"ann";

CustomAnnotationView* annotationView = (CustomAnnotationView*)[mapView 
    dequeueReusableAnnotationViewWithIdentifier:identifier];

//annotationView = nil;  // <-- remove this line

if (annotationView == nil) 
{
    annotationView = [[CustomAnnotationView alloc] 
                         initWithAnnotation:annotation 
                         reuseIdentifier:identifier];
    annotationView.enabled = YES;
    annotationView.canShowCallout = YES;
}
else
{
    annotationView.annotation = annotation;
}

//MOVE the setting of the image to AFTER the dequeue/create 
//because the image property of the annotation view 
//is based on the annotation and we can update image in one place
//after both the dequeue and create are done...

//Get the image name from the annotation ITSELF 
//from a custom property (that you add/set)
Annotation *myCustomAnn = (Annotation *)annotation;

NSString *imgName = myCustomAnn.imageName;  
//imageName is the custom property you added/set

UIImage *img = [UIImage imageNamed:
                   [NSString stringWithFormat:@"%@.png",imgName]];

annotationView.image = img;

return annotationView;

由Rob解決

問題是,我無法檢索在VC中設置的注釋屬性。 原因是我使用了[_mapView addAnnotation:ann]; 命令,然后設置所有注釋的屬性。 就是說,通過將這條線移動到注釋初始屬性設置的末尾,可以解決該問題。

謝謝大家,非常感謝Rob! 我要迎接下一個挑戰。

一個問題是viewForAnnotation根據類實例變量確定要顯示的正確圖像。 通常,注釋圖像的標識符將是自定義注釋本身的屬性,而不是某些外部實例變量。

最重要的是,似乎在設置所有注釋的屬性之前,已將注釋添加到地圖。 應該將addAnnotation推遲到所有注釋的屬性都設置好之后。

或者,您可以將注釋添加到NSMutableArray ,根據需要進行調整,然后僅使用addAnnotations (請注意s)在最后添加注釋,並將其傳遞給數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM