Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c MKViewAnnotation自定义批注在MKMapView中添加时失去顺序_Objective C_Arrays_Parsing_Mkmapview_Mkannotationview - Fatal编程技术网

Objective c MKViewAnnotation自定义批注在MKMapView中添加时失去顺序

Objective c MKViewAnnotation自定义批注在MKMapView中添加时失去顺序,objective-c,arrays,parsing,mkmapview,mkannotationview,Objective C,Arrays,Parsing,Mkmapview,Mkannotationview,我需要在MkMapView上显示大约10个位置和相应的自定义注释图像(取决于JSON解析加载的值)。正如中所建议的,我已经创建了一个自定义注释类来存储一些数据,但是,同样,我无法获得正确的顺序:每个地图位置上的自定义图像都不符合相应解析值的正确顺序,而在UITableView中则是完美的。这是简化代码: 通信示例: if parsed valuesID is 100 ---> annotation image must be 100.png if parsed valuesID is 20

我需要在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
- (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) ];
}
   - (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

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

@interface MyAnnotation2 : NSObject <MKAnnotation>

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

@end
#import "MyAnnotation2.h"

@implementation MyAnnotation2

@synthesize coordinate;
@synthesize valuesIDMyAnnotation2;

@end
- (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);
    }

}
- (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;

}
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
viewDidLoad方法:

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
- (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) ];
}
   - (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

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

@interface MyAnnotation2 : NSObject <MKAnnotation>

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

@end
#import "MyAnnotation2.h"

@implementation MyAnnotation2

@synthesize coordinate;
@synthesize valuesIDMyAnnotation2;

@end
- (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);
    }

}
- (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;

}
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
UIButton调用的parseMethod:

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
- (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) ];
}
   - (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

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

@interface MyAnnotation2 : NSObject <MKAnnotation>

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

@end
#import "MyAnnotation2.h"

@implementation MyAnnotation2

@synthesize coordinate;
@synthesize valuesIDMyAnnotation2;

@end
- (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);
    }

}
- (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;

}
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
MyAnnotation2.h自定义类:

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
- (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) ];
}
   - (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

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

@interface MyAnnotation2 : NSObject <MKAnnotation>

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

@end
#import "MyAnnotation2.h"

@implementation MyAnnotation2

@synthesize coordinate;
@synthesize valuesIDMyAnnotation2;

@end
- (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);
    }

}
- (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;

}
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
addAnnotations方法(在解析完成后调用):

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
- (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) ];
}
   - (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

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

@interface MyAnnotation2 : NSObject <MKAnnotation>

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

@end
#import "MyAnnotation2.h"

@implementation MyAnnotation2

@synthesize coordinate;
@synthesize valuesIDMyAnnotation2;

@end
- (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);
    }

}
- (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;

}
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
最后,viewForAnnotation代表:

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
- (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) ];
}
   - (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

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

@interface MyAnnotation2 : NSObject <MKAnnotation>

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

@end
#import "MyAnnotation2.h"

@implementation MyAnnotation2

@synthesize coordinate;
@synthesize valuesIDMyAnnotation2;

@end
- (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);
    }

}
- (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;

}
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()提供了位置和值SID的正确对应关系。相反,在MKMapView上,自定义注释图像未按正确的顺序添加,因此我有正确的注释图像,但位于错误的位置。请再次帮助我解决这个问题,谢谢

在每个
pinView.image
行上放置一个断点,当设置image200.png时,检查坐标是什么(您可能需要记录坐标,我从来都不擅长深入挖掘调试器数据)。如果存在不匹配的情况,请查看代码的其余部分,查看可能会更改位置值的任何内容,并在其中设置断点。如果在
parseMethod
viewForAnnotation
之间触发该断点,则可能是您的罪魁祸首。

在viewForAnnotation中,为所有三个ifs分配了相同的图像。这是故意的吗?是的@annakarenia,这是我的转录错误:现在我已经纠正了它,并添加了UITableView委托,以提供更精确和清晰的代码。谢谢另一个打字错误:
annotation2.valueIDMyAnnotation2=
应该是
annotation2.valuesIDMyAnnotation2=
(在
ID
之前缺少
s
)--如果复制并粘贴准确的代码而不是重新键入,效果会更好。无论如何,在发布的代码中没有明显的问题。唯一的可能性是
坐标
数组(表视图正在使用)与
位置
数组(注释正在使用)不匹配。哦,但这绝对是难以置信的。你在addAnnotations方法中看到for()循环了吗?在NSLog结果中,我可以读取,例如:“100-坐标lat1-lon1”,但在MKMapView上,具有lat1和lon1坐标的位置具有注释image200.png。。。我快昏了头!谢谢@Craig,我曾尝试添加断点,但同样无法理解:在
[map addAnnotation:annotation2]
之前,我已按正确顺序对所有数据进行了排序,正如您在NSLog结果中所看到的那样,因此我无法获得问题。断点的结果是什么?设置图像时,坐标在哪里,正确还是错误?嗯,@AnnaKarenina是正确的:经过几天的代码调试,我注意到只有在某些位置,
坐标
数组与正确的
位置
不匹配。。。问题已解决。您是否愿意解释两个数组如何不匹配?很简单,这是我的错误:我使用错误的纬度和经度值声明了一些
MakeLocation(lat,lon)
,因此相应的解析值与地图上的正确位置不匹配(在parseMethod中,要解析valuesID,我不使用lat、lon,而是使用相应的GeoNamesID数组)。