2013-02-21 14 views
5

W widoku MKMap dostosowałem pinezkę do adnotacji z obrazem. Ale niektóre szpilki są statyczne i nie pokazują danego obrazu.Pokazuje różne obrazy Pin w MKMapview

Używam - (MKAnnotationView *) mapView: (MKMapView *) mapView viewForAnnotation: (id) adnotacja, aby ustawić obraz pinezki.

Dodanie mój kod i ekranu tutaj:

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


if ([annotation isKindOfClass:[MKUserLocation class]]) 
    return nil; 

static NSString* AnnotationIdentifier = @"AnnotationIdentifier"; 
MKPinAnnotationView* pinView = [[MKPinAnnotationView alloc] 
           initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier]; 
pinView.animatesDrop=YES; 
pinView.canShowCallout=YES; 
    pinView.pinColor= MKPinAnnotationColorGreen; 

    pinView.enabled = YES; 
    pinView.canShowCallout = YES; 
    pinView.image=[UIImage imageNamed:@"bublerest.png"]; //here I am giving the image 





UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
[rightButton setTitle:annotation.title forState:UIControlStateNormal]; 
[rightButton addTarget:self 
       action:@selector(showDetails:) 
     forControlEvents:UIControlEventTouchUpInside]; 

    pinView.rightCalloutAccessoryView = rightButton; 

UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"rest_image2.png"]]; 
pinView.leftCalloutAccessoryView = profileIconView; 


return pinView; 
} 

enter image description here

pomysłów?

+0

Czy widzisz w ogóle swoje własne ikony map dla dowolnej pinezki, czy tylko niektóre pinezki ich nie pokazują? – Zhang

Odpowiedz

11

Jeśli chcesz inny obraz na MKAnnotationView niż "pin" jabłka, musisz użyć MKAnnotationView zamiast MKPinAnnotationView.

MKPinAnnotationView nie ma być dostosowywany w ten sposób i będzie wyświetlał pinezkę od czasu do czasu. Urządzenie Class Reference pokazuje, że tylko pinColor i animatesDrop powinny zostać zmienione.

Utracisz animację i cień PinAnnotationView.

+1

Podczas gdy inne odpowiedzi tutaj dają wszystkie sposoby na dostosowanie obrazu, tylko ten rzeczywiście stwierdza, czym jest problem i jak go naprawić. – Craig

+0

Mam ten problem (Apple pin obraz zamiast mojego obrazu pin) w iOS9. Po tym jak Twoja sugestia działa poprawnie! Dzięki – Fulkron

+0

Jeśli chcesz, aby zarówno szpilki Apple (iPin?), Jak i własny niestandardowy obraz były wyświetlane na tej samej mapie - użyj niestandardowej adnotacji i dodaj wskaźnik "typu" wymaganego adnotacjiView (zwykłego lub Pin). Następnie zdejmij widok adnotacji lub pinAnnotationView w zależności od typu i osobno obsłuż dwie sprawy. – Larry

1

Zamieszczanie przykładowego fragmentu, którego użyłem w ostatnim projekcie. Mam nadzieję że to pomoże.

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

Annotation *annotationInst = (Annotation*)annotation; 

MKAnnotationView *pinView = nil; 
if(annotation != mapView.userLocation) 
{ 
    static NSString *defaultPinID = @"pinId"; 
    pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; 

    if (pinView == nil){ 
     pinView = [[[MKAnnotationView alloc] 
        initWithAnnotation:annotation reuseIdentifier:defaultPinID]autorelease];   
    } 


    pinView.canShowCallout = YES; 
    pinView.image = [UIImage imageNamed:LOCATION_BUBBLE]; 

    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    pinView.rightCalloutAccessoryView = rightButton; 
}  
return pinView;  
} 
1

Użyj tego w viewForAnnotation

if ([annotation isKindOfClass:[MyAnnotation class]]) { 
    // try to dequeue an existing pin view first 
    static NSString* myAnnotationIdentifier = @"MyAnnotationIdentifier"; 

    // If an existing pin view was not available, create one 
    MKPinAnnotationView* customPinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:myAnnotationIdentifier]; 

    if ([(MyAnnotation *)annotation ann] == AnnotationTypeStart) { 
     NSLog(@"In Start"); 
     customPinView.enabled = NO; 
     customPinView.image = [UIImage imageNamed:@"begin.png"]; 
    } 
} 

gdzie MyAnnotation jest realizowany poniżej

typedef enum AnnotationType { 
    AnnotationTypeStart, 
    AnnotationTypeEnd, 
    AnnotationTypeWayPoint, 
} AnnotationType; 

@interface MyAnnotation : NSObject <MKAnnotation> { 
    CLLocationCoordinate2D coordinate; 
    NSString *title; 
    NSString *subtitle; 
    AnnotationType ann; 
} 
2

Tworzenie nieruchomości w protokole MKAnnotation który będzie używany do ustawiania kołka tu np

@interface AddressAnnotation1 : NSObject<MKAnnotation> { 
} 
@property (nonatomic, retain) NSString *mPinColor; 

W pliku wdrażania pliku .m

- (NSString *)pincolor{ 
    return mPinColor; 
} 

- (void) setpincolor:(NSString*) String1{ 
    mPinColor = String1; 
} 

Po dodaniu annotation ustawić również kolor pin.

#pragma mark annotation delegate 
- (MKAnnotationView *) mapView:(MKMapView *)mapView1 viewForAnnotation:(AddressAnnotation1 *) annotation 
{ 
    UIImage *anImage = nil; 

    MKAnnotationView *annView=(MKAnnotationView*)[mapView1 dequeueReusableAnnotationViewWithIdentifier:@"annotation"]; 
    if(annView==nil){ 
     annView=[[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotation"] autorelease]; 
    } 
    if([annotation.mPinColor isEqualToString:@"green"]) 
    { 
     anImage=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Google map pin 02.png" ofType:nil]]; 
    } 
    else if([annotation.mPinColor isEqualToString:@"red"]) 
    { 
     anImage=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Google map pin 01.png" ofType:nil]]; 
    } 
    else if([annotation.mPinColor isEqualToString:@"blue"]) 
    { 
     anImage=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Google map pin 02.png" ofType:nil]]; 
    } 
    annView.image = anImage; 
return annView; 
} 

Jeśli nie chcesz niestandardowe obrazy dla kołków zastąpić ciało instrukcji warunkowych z

annView.pinColor = MKPinAnnotationColorGreen; 

lub

annView.pinColor = MKPinAnnotationColorRed; 

lub

annView.pinColor = MKPinAnnotationColorPurple; 

I wprowadziły ten przed. Więc idź z tym.

+0

Dziękuję, to dobry pomysł :) –

+0

Ten kod powinien działać dobrze, ale w moim projekcie to nie działa. Również ładuję plik KML do mapy. –

Powiązane problemy