ios - 多个注释未显示(Xcode mapview)

标签 ios xcode ipad annotations android-mapview

[super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

NSArray *name = [NSArray arrayWithObjects:@"Marina Bay Sands", @"Sentosa", @"Singapore Polytechnic",nil];
NSArray *description = [NSArray arrayWithObjects:@"Got casino!!", @"Got Universal Studio Singapore!!", @"Best polytechnic in Singapore!",nil];    

self.mapAnnotations = [[NSMutableArray alloc] initWithCapacity:3];

//Set coordinates for pin
CLLocationCoordinate2D location;
location.latitude = (double)1.2822463547298561;
location.longitude = (double) 103.85830879211426;
MapPin *mapPin = [[MapPin alloc] init];
[mapPin setCoordinate:location];
[mapPin setName: [name objectAtIndex:0]];
[mapPin setDescription:[description objectAtIndex:0]];
[self.mapAnnotations addObject:mapPin];

location.latitude = (double) 1.249404;
location.longitude = (double) 103.830321;
[mapPin setCoordinate:location];
[mapPin setName: [name objectAtIndex:1]];
[mapPin setDescription:[description objectAtIndex:1]];
[self.mapAnnotations addObject:mapPin];

location.latitude = (double) 1.309976;
location.longitude = (double) 103.775921;
[mapPin setCoordinate:location];
[mapPin setName: [name objectAtIndex:2]];
[mapPin setDescription:[description objectAtIndex:2]];
[self.mapAnnotations addObject:mapPin];

[self.mapView addAnnotations:self.mapAnnotations];

self.mapView.mapType = MKMapTypeStandard;
[self location];
mapView.showsUserLocation = YES;

嗨,对于Xcode编程来说是很新的东西,这是我在viewDidLoad中的代码。我正在尝试显示多个注释,这只是为了尝试。但是从这些代码中,我只能显示1个注解,这是我添加到 mapAnnotations 数组中的最后一个对象。

我可以使它起作用的唯一方法是初始化 mapPin1 mapPin2 ,而不是对我要创建的其他两个注释全部使用 mapPin 。但是这样看来效率很低(如果我写错了,请纠正我),而且,我要添加数百个数据。可能无法对其进行硬编码。

有人可以帮我吗?

谢谢=)

最佳答案

我已经在您的代码中添加了注释,以显示实际情况。

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

NSArray *name = [NSArray arrayWithObjects:@"Marina Bay Sands", @"Sentosa", @"Singapore 
    Polytechnic",nil];
NSArray *description = [NSArray arrayWithObjects:@"Got casino!!", @"Got Universal Studio 
    Singapore!!", @"Best polytechnic in Singapore!",nil];    

self.mapAnnotations = [[NSMutableArray alloc] initWithCapacity:3];

//here you create the properties of the pin the first time
CLLocationCoordinate2D location;
location.latitude = (double)1.2822463547298561;
location.longitude = (double) 103.85830879211426;

//here you create the pin
MapPin *mapPin = [[MapPin alloc] init];

//now you set its properties
[mapPin setCoordinate:location];
[mapPin setName: [name objectAtIndex:0]];
[mapPin setDescription:[description objectAtIndex:0]];
//now you add it to the array
[self.mapAnnotations addObject:mapPin];

//now you are changing the properties of `mapPin`
location.latitude = (double) 1.249404;
location.longitude = (double) 103.830321;
[mapPin setCoordinate:location];
[mapPin setName: [name objectAtIndex:1]];
[mapPin setDescription:[description objectAtIndex:1]];

//the problem with adding mapPin again here is that you are adding
//the same object again, just with different properties, so
//your array still only has one object in it, `mapPin`, but with its updated properties
[self.mapAnnotations addObject:mapPin];

//you change the properties again here
location.latitude = (double) 1.309976;
location.longitude = (double) 103.775921;
[mapPin setCoordinate:location];
[mapPin setName: [name objectAtIndex:2]];
[mapPin setDescription:[description objectAtIndex:2]];

//and the same phenomenon I previously described happens again here
[self.mapAnnotations addObject:mapPin];

[self.mapView addAnnotations:self.mapAnnotations];

self.mapView.mapType = MKMapTypeStandard;
[self location];
mapView.showsUserLocation = YES;

如果您不想在每次将新的mapPins添加到数组中时都创建新的mapPins,请在每次将图钉添加到数组中时尝试执行以下操作:

代替
[self.mapAnnotations addObject:mapPin];

尝试
[self.mapAnnotations addObject:[[mapPin copy] autorelease]];

这将添加修改后的mapPin的副本,而不是指向原始副本所在的内存中的相同空间。

关于ios - 多个注释未显示(Xcode mapview),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8930084/

相关文章:

php - 如何通过 iOS 应用程序将图像插入远程数据库?

ios - Apple Watch 模拟器不响应推送通知 Payload.apns 中的更改

ios - 如何跳过传入 [NSString stringWithFormat :] 的数据

iphone - iPod运行4.2背景图片有轻微错误,当iPhone模拟器4.2正常

iphone - 从 Xcode map 上移除多段线

iphone - 保护 Objective C 源代码中的敏感信息

iphone - 在 iPad 上播放声音,使用 AudioToolbox

ios - 带有 IOS 的 Google IMA HTML5 SDK

Xcode 8扩展执行NSTask

iphone - 如何查找数组中的重复值?