我需要在我的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方法(在解析的COMPLETE 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);
}
}
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上它的所有PERFECT,我可以看到位置坐标和自定义图像之间的正确对应关系,并且NSLog()给出了位置和值ID的正确对应关系。相反,在MKMapView上,自定义注释图像没有以正确的顺序添加,因此我有正确的注释图像,但位置错误。请再次帮我解决这个问题,谢谢!
答案 0 :(得分:0)
在每个pinView.image
行上设置一个断点,当它设置image200.png时,检查坐标是什么(你可能需要NSLog它们,我从来没有很好地深入挖掘调试器数据)。如果你有一个不匹配的地方,你可以查看其他任何可以改变位置值的东西代码,其中包括任何东西,并在那里设置一个断点。如果在parseMethod
和viewForAnnotation
之间触发该断点,那么您可能有罪魁祸首。