After JSON parsing -viewForAnnotation shows only ONE single annotation on MKMapView

萝らか妹 提交于 2019-11-28 14:23:23

In parseMethod, change this:

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

to this:

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

Move the MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init] to inside the for-loop so that a new annotation object is created for each location.

The existing code there creates a single annotation object and just keeps modifying its coordinate.

What is also really important to note (because it was very hard to find that error and I sat the whole last night in front of this...) is to check if your object that conforms to MKAnnotation has some master base class that imlements the methods isEqual and / or hash on only the data of the base class and you do not overwrite that in your base class. This was my problem and thus isEqual returned YES for all annotations and THUS it showed always only one annotation on the map. I couldn't find any hint on the internet about this so I will leave at least this comment here for poor people in the same situation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!