【问题标题】:MKViewAnnotation custom annotations losing order when added in MKMapViewMKViewAnnotation 自定义注释在添加到 MKMapView 时失去顺序
【发布时间】:2013-02-08 12:34:20
【问题描述】:

我需要在我的 MkMapView 上显示大约 10 个位置和相应的自定义注释图像(取决于 JSON 解析 加载的值)。正如previous answers 中所建议的那样,我创建了一个自定义注释类来存储一些数据,但同样,我无法获得正确的顺序:每个地图位置上的自定义图像不尊重各自解析值的正确顺序,而在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;

}

EDIT - 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() 给出了位置和 valuesID 的正确对应关系。相反,在 MKMapView 上,自定义注释图像没有按正确的顺序添加,所以我有正确的注释图像,但位置错误。请再次帮我解决这个问题,谢谢!

【问题讨论】:

  • 在 viewForAnnotation 中,为所有三个 if 分配了相同的图像。这是故意的吗?
  • 是的@AnnaKarenina,这是我的转录错误:现在我已更正它并添加了 UITableView 委托,以提供更精确和清晰的代码。谢谢!
  • 另一个错字:annotation2.valueIDMyAnnotation2 = 应该是annotation2.valuesIDMyAnnotation2 = (在ID 之前缺少s)- 如果您复制+粘贴确切的代码而不是重新键入会更好。无论如何,发布的代码中没有明显的问题。唯一的可能是coordinates 数组(表视图正在使用)与locations 数组(注释正在使用)不匹配。
  • 哦,但这绝对令人难以置信。你看到 -addAnnotations 方法中的 for () 循环了吗?在 NSLog 结果中我可以读取,例如:“100 - COORDINATES lat1 - lon1”,但在 MKMapView 上,具有 lat1 和 lon1 坐标的位置有注释 image200.png... 我快晕了!

标签: objective-c arrays parsing mkmapview mkannotationview


【解决方案1】:

在每个pinView.image 行上放置一个断点,并在设置 image200.png 时检查坐标是什么(您可能需要对它们进行 NSLog,我从来不擅长深入调试器数据)。如果您有不匹配的情况,请查看其余代码中可能会更改位置值的任何其他内容,任何内容,然后在此处设置一个断点。如果在parseMethodviewForAnnotation 之间触发了该断点,那么您可能就是罪魁祸首。

【讨论】:

  • 谢谢@Craig,我曾尝试添加断点,但同样无法理解:在[map addAnnotation:annotation2] 之前,我已按照正确的顺序对所有数据进行了排序,因为你可以在 NSLog 结果中看到,所以我无法解决问题。
  • 断点的结果是什么?设置图像时的坐标在哪里,正确还是错误?
  • 好吧,@AnnaKarenina 是对的:经过几天的代码调试,我注意到仅在某些位置,coordinates 数组与正确的 locations 不匹配...问题已解决。
  • 您愿意解释一下这两个数组是如何不匹配的吗?
  • 简单,这是我的错误:我声明了一些 MakeLocation(lat,lon) 的纬度和经度值错误,因此对应的解析值与地图上的正确位置不匹配(在 parseMethod 中,解析 valuesID,我不使用 lat,lon 而是使用相应的 GeoNamesID 数组)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多