0

MkMapView に約 10 の場所とそれぞれのカスタム注釈画像を表示する必要があります ( JSON 解析によって読み込まれた値に応じて異なります)。以前の回答で示唆されたように、データを保存するカスタム アノテーション クラスを作成しましたが、正しい順序を取得できません。各マップの場所のカスタム画像は、UITableView 内にある間、それぞれの解析された値の正しい順序を尊重しません。そのすべてが完璧です。これは単純化されたコードです:

対応例:

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 メソッド (解析の完了後に呼び出されます):

- (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 では、カスタム注釈画像が正しい順序で追加されないため、正しい注釈画像がありますが、間違った場所にあります。この問題を解決するためにもう一度助けてください、ありがとう!

4

1 に答える 1

0

各行にブレークポイントを置き、pinView.imageimage200.png を設定するときに座標を確認します (NSLog が必要になる場合があります。デバッガーのデータを深く掘り下げるのは得意ではありません)。不一致がある場合は、場所の値を変更している可能性のある他のコードの残りの部分を調べて、そこにブレークポイントを置きます。そのブレークポイントが と の間でトリガーされた場合parseMethodviewForAnnotation犯人がいる可能性があります。

于 2013-02-09T03:13:46.280 に答える