2012-01-19 41 views
1
[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数组中的最后一个对象。多个注释不显示(Xcode mapview)

我能得到它的工作的唯一办法是初始化mapPin1mapPin2,而是采用mapPin所有,为我要创建的其他两个注解。但是这样看来效率很低(如果我错了,就纠正我),而且我有数百个数据要添加进来。不可能对它们进行硬编码。

有人可以帮助我吗?

谢谢=)

回答

3

你正在创建一个mapPin并多次添加相同的对象。您需要alloc/init这是您希望添加到地图中的每个注释的新注释对象。

0

的线索尝试

int howManyAreThere = [self.mapAnnotations count]; 

后您3添加和断点它看到什么是真正发生。

虽然NSMutableArray处理重复它不会复制您传递它的对象。 它比那个更聪明

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的副本,而不是指向同一个SPAC记忆中的原始坐标。

+0

哦谢谢!它现在工作得很好! =) – mLjH

+0

很高兴我能帮到你。如果我的答案解决了您的问题,请将其标记为这样! –

0

采取一切纬度长数组中,并使用下面的代码

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