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