[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 用于所有人,而不是我要创建的两个注释。但这种方式看起来效率很低(如果我错了就纠正我),而且,我还要添加数百个数据。无法对它们进行硬编码。
有人可以帮我吗?
谢谢=)
答案 0 :(得分:4)
我在您的代码中添加了注释,显示了真正正在发生的事情。
[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,请在每次将引脚添加到数组时尝试使用此地图:
而不是
[self.mapAnnotations addObject:mapPin];
试
[self.mapAnnotations addObject:[[mapPin copy] autorelease]];
这将添加修改后的mapPin的副本,而不是指向原始内容所在的内存中的相同空间。
答案 1 :(得分:3)
您正在创建一个mapPin
并重复添加相同的对象。您需要为要添加到地图的每个注释alloc/init
一个新的注释对象。
答案 2 :(得分:0)
试试线索
int howManyAreThere = [self.mapAnnotations count];
在你的3添加和断点之后,看看实际发生了什么。
虽然NSMutableArray处理重复项,但它不会复制传递它的对象。 它比那更聪明。
答案 3 :(得分:0)
取数组中的所有lat long并使用下面的代码
for (int i = 0; i < [arrListing count]; i++) {
List *obj = [arrList objectAtIndex:i];
NSLog(@"Title %@ long:%@ Lat:%@",obj.Title,obj.log,obj.lat);
CLLocationCoordinate2D annotationCoord;
annotationCoord.latitude = [obj.lat floatValue];
annotationCoord.longitude = [obj.log floatValue];
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = annotationCoord;
annotationPoint.title = obj.Title;
annotationPoint.subtitle = obj.log;
[mapView addAnnotation:annotationPoint];
}