2012-01-19 28 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マップビュー)

私は仕事にそれを得ることができる唯一の方法は、私が作成したい他の二つの注釈のため、代わりにすべてのためマッピンを使用しての、mapPin1mapPin2を初期化することでした。しかし、この方法では非常に非効率的で(私が間違っている場合は私を修正してください)、さらに、私は追加するデータが何百もあります。

誰かが私にこれを手伝ってもらえますか?

は、あなたが1 mapPinを作成し、繰り返し同じオブジェクトを追加している=)

答えて

3

をありがとうございました。マップに追加するアノテーションごとに新しいアノテーションオブジェクトがalloc/init必要です。あなたの3が追加され、実際に何が起こっているかを確認するために、それをブレークポイント後の手がかりのために

0

int howManyAreThere = [self.mapAnnotations count]; 

を試してみてください。

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

これは、代わりに同じSPACを指しているの、あなたの修正マッピンのコピーを追加します

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

を試してみてください元のものが座っている場所に記憶されています。

+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]; 
     } 
関連する問題