2013-02-08 78 views
0

我需要在我的MkMapView上顯示約10個位置和相應的自定義註釋圖像(取決於由JSON解析加載的值)。正如previous answers中所建議的,我創建了一個自定義註記類來存儲一些數據,但是我再次無法獲得正確的順序:每個地圖位置上的自定義圖像並不尊重各自分析值的正確順序,而在UITableView中全部完美。這是簡化的代碼:MKViewAnnotation自定義註釋在MKMapView中添加時丟失順序

對應的例子:

if parsed valuesID is 100 ---> annotation image must be 100.png 
if parsed valuesID is 200 ---> annotation image must be 200.png 
if parsed valuesID is 300 ---> annotation image must be 300.png 

viewDidLoad方法:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    map.showsUserLocation = true; 
    map.mapType = MKMapTypeStandard; 

    #define MakeLocation(lat,lon) [[CLLocation alloc] initWithLatitude:lat longitude:lon] 

    locations= @[ MakeLocation(lat1,lon1), MakeLocation(lat2,lon2), MakeLocation(lat3,lon3), MakeLocation(lat4,lon4), MakeLocation(lat5,lon5), MakeLocation(lat6,lon6), MakeLocation(lat7,lon7), MakeLocation(lat8,lon8), MakeLocation(lat9,lon9), MakeLocation(lat10,lon10) ]; 
} 

由一個UIButton調用的parseMethod:

- (IBAction)parseMethod { 

     [map removeAnnotations:map.annotations]; 

     // THE COMPLEX CODE TO PARSE VALUES of valuesID 
     ... 
     ... // so here I have the full array of valuesID 
     ... 
     // THE CONTROL FOR THE END OF COMPLETE PARSING (blocks, cycle, ...) 

     [self addAnnotations]; // here i'm sure to call method AFTER THE END of complete parsing 

    } 

的MyAnnotation2.h自定義類:

#import <Foundation/Foundation.h> 
#import <MapKit/MapKit.h> 

@interface MyAnnotation2 : NSObject <MKAnnotation> 

@property (nonatomic, assign) CLLocationCoordinate2D coordinate; 
@property (nonatomic, assign) int valuesIDMyAnnotation2; 

@end 

的MyAnnotation2.m自定義類:

#import "MyAnnotation2.h" 

@implementation MyAnnotation2 

@synthesize coordinate; 
@synthesize valuesIDMyAnnotation2; 

@end 

的addAnnotations方法(稱爲解析的COMPLETE結束後):

- (void)addAnnotations { 

    [table reloadData]; // UITableView with rows populated with locations coordinates and respective valuesID 
    [table scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES]; 

    for (int l=0; l<[locations count]; l++) { 

     annotation2 = [[MyAnnotation2 alloc] init]; // create MyAnnotation2 istance to assign custom properties 
     annotation2.valuesIDMyAnnotation2 = [[valuesID objectAtIndex:l] intValue]; 
     annotation2.coordinate = [locations[l] coordinate]; 
     [map addAnnotation: annotation2]; // here we call delegate with all necessary data to add annotations, both location coordinate and corresponding valuesID 

     NSLog(@"%d - COORDINATES: %f - %f",annotation2.valuesIDMyAnnotation2,annotation2.coordinate.latitude, annotation2.coordinate.longitude); 
    } 

} 

的UITableView的委託

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle: 
       UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"]; 
    } 
      cell.textLabel.text = [NSString stringWithFormat:@"%@",[coordinates objectAtIndex:indexPath.row]]; // here coordinates are values from each location 

      if ([[valuesID objectAtIndex:indexPath.row] intValue] == 100) { 
       UIImage *image = [UIImage imageNamed:@"100.png"]; 
       [cell.imageView setImage:image]; 
      } 
      if ([[valuesID objectAtIndex:indexPath.row] intValue] == 200) { 
       UIImage *image = [UIImage imageNamed:@"200.png"]; 
       [cell.imageView setImage:image]; 
      } 
      if ([[valuesID objectAtIndex:indexPath.row] intValue] == 300) { 
       UIImage *image = [UIImage imageNamed:@"300.png"]; 
       [cell.imageView setImage:image]; 
      } 
    return cell 
} 

最後,viewForAnnotation委託:

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

    if (! [annotation isKindOfClass:[MyAnnotation2 class]]) 
    { 
     ((MKUserLocation *)annotation).title = @"My position"; 
    return nil; 
    } 

    MKAnnotationView *pinView= [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pinView"]; 

    MyAnnotation2 *myPin = (MyAnnotation2 *)annotation; 

    if (myPin.valuesIDMyAnnotation2 == 100) { 
     pinView.image = [UIImage imageNamed:@"100.png"]; 
    } 
    if (myPin.valuesIDMyAnnotation2 == 200) { 
     pinView.image = [UIImage imageNamed:@"200.png"]; 
    } 
    if (myPin.valuesIDMyAnnotation2 == 300) { 
     pinView.image = [UIImage imageNamed:@"300.png"]; 
    } 
    [pinView setFrame:CGRectMake(0, 0, 25, 25)]; 
    return pinView; 

} 

編輯 - 例如NSLogs結果(代碼從addAnnotations法):

100 - COORDINATES lat1 - lon1 // here I expect annotation images100.png on location1 
200 - COORDINATES lat2 - lon2 // ... 
100 - COORDINATES lat3 - lon3 
300 - COORDINATES lat4 - lon4 
100 - COORDINATES lat5 - lon5 
200 - COORDINATES lat6 - lon6 
100 - COORDINATES lat7 - lon7 
300 - COORDINATES lat8 - lon8 
300 - COORDINATES lat9 - lon1 
200 - COORDINATES lat10 - lon10 

結果: 在UITableView上它的所有完成,我可以看到位置座標和自定義圖像之間的正確對應關係,並且NSLog()也給出了位置和值ID的正確對應關係。在MKMapView上,相反,自定義註釋圖像沒有添加在正確的序列,所以我有正確的註釋圖像,但在錯誤的位置。請幫助我再次解決此問題,謝謝!

+0

在viewForAnnotation中,爲所有三個if分配了相同的圖像。這是故意的嗎? – Anna 2013-02-08 13:14:59

+0

是@AnnaKarenina,它一直是我的轉錄錯誤:現在我已經糾正它,並且還添加了UITableView委託,以提供更精確和清晰的代碼。謝謝! – Huxley 2013-02-08 13:27:10

+1

另一個拼寫錯誤:'annotation2.valueIDMyAnnotation2 ='應該是'annotation2.valuesIDMyAnnotation2 ='(在'ID'前面缺少's') - 如果您複製+粘貼確切的代碼而不是重新輸入會更好。無論如何,在發佈的代碼中沒有明顯的問題。唯一的可能性是'coordinates'數組(使用表視圖)與'locations'數組(註釋正在使用)不匹配。 – Anna 2013-02-08 14:09:31

回答

0

在每個pinView.image行上設置一個斷點,當它設置image200.png時,檢查座標是什麼(您可能需要NSLog它們,我從未擅長深入調試數據)。如果你有一個不匹配的地方,那麼查看其他任何可能改變位置值的東西的代碼,任何東西,並在那裏放置一個斷點。如果該斷點在parseMethodviewForAnnotation之間被觸發,那麼你可能有你的罪魁禍首。

+0

謝謝@Craig,我試圖添加斷點,但是,再次無法理解:在[[map addAnnotation:annotation2]之前]我已經按照正確的順序排序了所有數據在NSLog結果中,所以我無法解決問題。 – Huxley 2013-02-11 11:40:14

+0

破發點的結果是什麼?圖像被設置時的座標,正確或不正確的地方? – Craig 2013-02-11 19:26:43

+0

嗯,@AnnaKarenina是正確的:經過幾天的代碼調試後,我注意到只有對於某些位置,'coordinates'數組與'right'位置不匹配......問題已解決。 – Huxley 2013-02-12 10:42:40