2011-03-30 22 views
7

Hey, haritanıza bir açıklama eklemeyi deniyorum. Nasıl yapabilirim?MKMapView'te Harita ek açıklaması nasıl eklenir?

İşte benim kod:

- (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); 

[self.view addSubview:mapa]; 


} 

teşekkürler!

cevap

16

MKAnnotation bir protokol olduğundan, protokolü uygulayan kendi sınıfınızı tanımlamanız gerekir. Eğer bir enlem varsa Örneğin,

@interface SPAnnotation : NSObject <MKAnnotation> { 
    CLLocationCoordinate2D coordinate; 
} 

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate; 

, konumunun boylam eşlemek istiyorum:

SPAnnotation *annotation = [[Annotation alloc] initWithCoordinate:CLLocationCoordinate2DMake(latitude, longitude)]; 
[myMap addAnnotation:annotation]; 
+0

Im alma " 'Annotation' bildirilmemiş tanımlayıcı –

+0

Kullanımı MKAnnotation bir protokol olduğundan, kendi Ek Açıklama sınıfını tanımlamak gerekecek. Bu cevaba bakın: http://stackoverflow.com/questions/2878758/iphone-create-mkannotation –

+6

Ayrıca, önceden tanımlanmış MKPointAnnotation sınıfını da kullanabilirsiniz: – Anna

0

Ayrıca MKAnnotation protokolünü benimseyen bir ek açıklama sınıfını kodlamak gerekecek . örneğin:

@interface MyMapAnnotation : NSObject <MKAnnotation> { 
... 
} 
+0

Aynı başlıkta yapmak mı istiyorsunuz? –

+0

Ek açıklığınız için kendi üstbilgisi ve ana dosyasıyla yeni bir sınıf oluşturmak isteyeceksiniz. En azından, bu sınıfın - (id) initWithCoords: (CLLocationCoordinate2D) coords ve CLLocationCoordinate2D koordinat özelliğini uygulaması gerekir. Bakınız: [MKAnnotation docs] (http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKAnnotation_Protocol/Reference/Reference.html) – jmans

0

deneyin bu

Pin *selected = [[Pin alloc] init]; 

for(i=0; i<[[newxml dataarray] count]; i++) 
{ 
    gapi = [newxml.dataarray objectAtIndex:i]; 
    MKCoordinateRegion region ; // = { {0.0, 0.0 }, { 0.0, 0.0 } }; 
    region.center. latitude = [gapi.lat floatValue]; 
    region.center. longitude = [gapi.lng floatValue]; 

    region.span.longitudeDelta = 0.05f; 
    region.span.latitudeDelta = 0.05f; 

    [self.mapview setRegion:region animated:YES]; 

    Pin* myAnnotation1=[[Pin alloc] init]; 

    myAnnotation1.coordinate=region.center;; 
    myAnnotation1.title=gapi.name; 
    myAnnotation1.itemIndex = i; 
    myAnnotation1.subtitle=gapi.address; 
    [self.mapview addAnnotation:myAnnotation1]; 

    if (j == i) 
    { 
      selected = myAnnotation1; 
    } 

    if(k==123) 
    { 
     [self.mapview selectAnnotation:selected animated:NO]; 
    } 

}

İlgili konular