2011-03-30 34 views
5

Mümkün mü?Resim ekle MKPointAnnotation'a ekle

Burada bana bir iğne veren bir eylem gerçekleştiriyorum. Ama bir görüntüye ihtiyacım var. Ve MKAnnotation benim için karmaşık görünüyor.

 - (void)abreMapa:(NSString *)endereco { 

      NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", 
            [endereco stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
      NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]]; 
      NSArray *listItems = [locationString componentsSeparatedByString:@","]; 

      double latitude = 0.0; 
      double longitude = 0.0; 

      if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) { 
       latitude = [[listItems objectAtIndex:2] doubleValue]; 
       longitude = [[listItems objectAtIndex:3] doubleValue]; 
      } 
      else { 
       //Show error 
      } 

      CLLocationCoordinate2D coordinate; 
      coordinate.latitude = latitude; 
      coordinate.longitude = longitude; 
      myMap.region = MKCoordinateRegionMakeWithDistance(coordinate, 2000, 2000); 



      MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init]; 
      [annotation setCoordinate:coordinate]; 
      [annotation setTitle:@"Some Title"]; 
      [myMap addAnnotation:annotation]; 




      // Coloca o icone 
      [self.view addSubview:mapa]; 


     } 

Teşekkürler!

cevap

22

Bir MKMapViewDelegate kurmak gerekir ve İşte Apple'ın geliştirici sitesinde sağlanan MapCallouts örnek kod çalınan örnek kod, var

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation 

uygulamak gerekir. Önemli ayrıntılara odaklanmak için değiştirdim. Aşağıda, görüntünün bir ek açıklama görünümünde ayarlanması ve bu ek açıklama görünümünün bu yöntemden döndürülmesi olduğunu görebilirsiniz.

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
     static NSString *SFAnnotationIdentifier = @"SFAnnotationIdentifier"; 
     MKPinAnnotationView *pinView = 
      (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:SFAnnotationIdentifier]; 
     if (!pinView) 
     { 
      MKAnnotationView *annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation 
                      reuseIdentifier:SFAnnotationIdentifier] autorelease]; 
      UIImage *flagImage = [UIImage imageNamed:@"flag.png"]; 
      // You may need to resize the image here. 
      annotationView.image = flagImage; 
      return annotationView; 
     } 
     else 
     { 
      pinView.annotation = annotation; 
     } 
     return pinView; 
} 

Bizim açıklama görüşlerin yeniden kullanımını sağlamak için zaten oluşturulmuş bir görünüm kapmak için dequeueReusableAnnotationViewWithIdentifier kullanın. Eğer biri iade edilmezse yeni bir tane yaratırız. Bu, yüzlerce MKAnnotationViews oluşturmamızı engelliyor, eğer sadece birkaç tanesi aynı anda görünürse.

+0

Bu MKMapViewDelegate benim için görünmüyor. –

+0

http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapViewDelegate_Protocol/MKMapViewDelegate/MKMapViewDelegate.html. Muhtemelen MKMapView'ün temsilci özelliğini onu yöneten denetleyiciye ayarlayacaksınız. –

+0

Bu basit uygulamayı nasıl uygulayabileceğimi gösterebilen basit bir örnek dosya var mı? –