1

我需要在我的 MkMapView 上显示大约 10 个位置(以及相应的注释),按下按钮后,我需要根据不同的 JSON 解析结果添加新的不同注释(例如 locationIDValue < 100 表示红色引脚,否则为绿色)。这是简化的代码:

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

    arrayID = [[NSMutableArray alloc] initWithObjects: @"id1", @"id2", @"id3", @"id4", @"id5", @"id6", @"id7", @"id8", @"id9", @"id10", nil]; 

    #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) ];

    for (int l=0; l<[locations count]; l++) { // HERE ITS PERFECT! I CAN SEE ALL 10 ANNOTATIONS!
        MKPointAnnotation* annotation= [MKPointAnnotation new];
        annotation.coordinate = [locations[l] coordinate];
        [map addAnnotation: annotation];
    }
}

   - (IBAction)parseMethod {

        [map removeAnnotations:map.annotations];

        for (int i=0; i < [arrayID count]; i++) { // arrayID contains ID values to parse for each location

            NSURL *url = [NSURL URLWithString:
                          [NSString stringWithFormat:
                           @"http://JSONPARSINGURL/%@",[arrayID objectAtIndex:i]]];

            NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:5.0];
            operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

        // PARSING CODE . . .

                NSMutableArray *value = [JSON objectForKey:@"value"];
                NSMutableDictionary *value0 = [value objectAtIndex:0];
                [valueID replaceObjectAtIndex:i withObject:[value0 objectForKey:@"valueID"]];
                locationIDValue = [[valueID objectAtIndex:i] intValue]; // locationIDValue contains the values that i must use to put different annotations on the map

                NSLog(@"locationIDValue: %d", locationIDValue); // here I control IF app parses all the values of locationIDValue

                [table reloadData]; // here I put another control to see all values parsed

            }
                                                                        failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
                                                                            NSLog(@"FAILURE");
                                                                        }];

            NSOperationQueue *queue = [[NSOperationQueue alloc] init]; // I have used also simple [operation start] but I have same issue!
            [queue addOperation:operation];
            [queue waitUntilAllOperationsAreFinished];

    }

        NSLog(@"END PARSING"); // here I control the end of parsing, so now I can add new annotations to MKMapView according to locationIDValue array

        MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
        for (int l=0; l<[locations count]; l++) { // HERE I CAN SEE ONLY A SINGLE LOCATION!
            annotation.coordinate = [locations[l] coordinate];

            NSLog(@"%f - %f", annotation.coordinate.latitude, annotation.coordinate.longitude); // here I control IF app parses all the values of coordinates

            [map addAnnotation: annotation];
        }

    }

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

    MKPinAnnotationView *pinView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pinView"];
    pinView.animatesDrop=YES;

    if (annotation != map.userLocation)
    {
        if ( locationIDValue <= 100) {
            pinView.pinColor = MKPinAnnotationColorRed;
            return pinView;
        }
        pinView.pinColor = MKPinAnnotationColorGreen;
        return pinView;
    }
    else
    map.userLocation.title = @"My position";
    return nil;
}

情况: 当我第一次打开应用程序时,我可以看到地图上的所有注释,根据#define MakeLocation纬度和经度;但是如果我按下按钮并启动 parseMethod,我等待几秒钟(根据 NSOperationQueue,但我也可以在没有队列的情况下测试它,使用简单的 [操作开始] 代码),然后我只能在地图上看到一个注释,总是一样的,奇怪的是在第五个位置,带有 lat5-lon5 坐标(否则,如果我更改一些代码,我可以看到所有注释都落在同一个位置)。如您所见,我编写了一些 NSLog 代码并添加 UITableView 来控制活动,结果如下:

END PARSING
coordinates: lat1 - lon1
coordinates: lat2 - lon2
coordinates: lat3 - lon3
coordinates: lat4 - lon4
coordinates: lat5 - lon5   <--- THE STRANGE POSITION
coordinates: lat6 - lon6
coordinates: lat7 - lon7
coordinates: lat8 - lon8
coordinates: lat9 - lon9
coordinates: lat10 - lon10
locationIDValue: 100
locationIDValue: 90
locationIDValue: 50
locationIDValue: 120
locationIDValue: 20
locationIDValue: 40
locationIDValue: 80
locationIDValue: 180
locationIDValue: 140
locationIDValue: 10

好吧,看起来很完美:应用程序解析所有数据(也是一个 UITableView 填充行,其中包含所有解析的新值),所以在解析操作结束后,我认为我们拥有用新注​​释填充地图所需的每个值。好吧,这里有什么问题?为什么解析后我只能看到一个注释?或者,所有注释都放在同一个位置?请给我一个帮助!

4

2 回答 2

3

parseMethod中,改变这个:

MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
for (int l=0; l<[locations count]; l++) { // HERE I CAN SEE ONLY A SINGLE LOCATION!
    annotation.coordinate = [locations[l] coordinate];

    NSLog(@"%f - %f", annotation.coordinate.latitude, annotation.coordinate.longitude); // here I control IF app parses all the values of coordinates

    [map addAnnotation: annotation];
}

对此:

for (int l=0; l<[locations count]; l++) { // HERE I CAN SEE ONLY A SINGLE LOCATION!

    //create a NEW annotation for each location...
    MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];

    annotation.coordinate = [locations[l] coordinate];

    NSLog(@"%f - %f", annotation.coordinate.latitude, annotation.coordinate.longitude); // here I control IF app parses all the values of coordinates

    [map addAnnotation: annotation];
}

MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init]to 移到for 循环,以便为每个位置创建一个新的注释对象。

那里的现有代码创建了一个注释对象并不断修改其坐标。

于 2013-02-05T18:35:29.383 回答
0

还有一点需要注意(因为很难找到那个错误,我昨晚整晚都坐在这个前面......)是检查你的符合 MKAnnotation 的对象是否有一些主基类来实现方法 isEqual 和/或仅对基类的数据进行散列,并且您不会在基类中覆盖它。这是我的问题,因此 isEqual 对所有注释都返回 YES,因此它在地图上始终只显示一个注释。我在互联网上找不到任何关于此的提示,因此我至少会在此处为处于相同情况的穷人留下此评论。

于 2015-10-06T08:22:56.650 回答