2012-01-19 63 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]; 
     }