2014-04-23 6 views
1

이 문제는 이미 2 주 동안 나를 괴롭 히고 있습니다!사용자 정의 다중 핀을 표시하면 위치가 잘못된 핀이 표시됩니다.

나는 탭바 응용 프로그램이 있습니다. 하나의 탭에는 점을 입력하고 다른 탭에는 점이 맵에 표시됩니다. 핀은 점의 유형에 따라 달라야합니다.

내가 직면하는 문제는 한 탭에서 다른 탭으로 전환 할 때마다 핀 이미지가 다른 이미지로 바뀌어야한다는 것입니다. 예를 들어지도에 4 점, 3 점이 원으로 표시되고 3 점이 삼각형으로 표시되면 삼각형이 한 지점에서 다른 지점으로 이동합니다. 심상은 확실히 무작위로 변화하는 것처럼 보입니다.

ViewController.m이 그것을

-(void) viewWillAppear:(BOOL)animated 
{ 
    // Select the type of map 
    if (isMapSelected == NO) { 
     self.mapView.mapType = MKMapTypeSatellite; 
    } 

    else { 
     self.mapView.mapType = MKMapTypeStandard; 
    } 


    // Add region to the map (center and span) 
    [self addRegion]; 

    // Removing old annotation 
    [self.mapView removeAnnotations:mapLocations]; 

    // Initializing arrays for the annotations 
    mapLocations = [[NSMutableArray alloc]init]; 

    [self addAnnotation]; 
} 


-(void) addAnnotation 
{ 

    CLLocationCoordinate2D mapLocation; 
    IGAMapAnnotation *mapAnnotation; 

    // Calculate how many points are included 
    NSInteger numberOfPoints = [coordinatesTempArray count]; 

    // Annotations will be added only of the flight plan includes at least one point 
    if (numberOfPoints > 0) 
    { 
    // Trying to add coordinates from the array of coordinates 
    for (NSInteger i=0; i < ([coordinatesTempArray count]); i++) { 

     mapAnnotation = [[IGAMapAnnotation alloc]init]; 

     // Taking a point in the array and getting its coordinates 
     self.mapCoordinates = [coordinatesTempArray objectAtIndex:i]; 

     // Getting a point in the array and getting its lattitude and longitude 
     self.mapLatitude = [[self.mapCoordinates objectAtIndex:0]doubleValue]; 
     self.mapLongitude = [[self.mapCoordinates objectAtIndex:1]doubleValue]; 

     // Assigning the point coordinates to the coordinates to be displayed on the map 
     mapLocation.latitude = self.mapLatitude; 
     mapLocation.longitude = self.mapLongitude; 

     // Adding coordinates and title to the map annotation 
     mapAnnotation.coordinate = mapLocation; 
     mapAnnotation.title = [navaidNamesTempArray objectAtIndex:i]; 
     mapAnnotation.subtitle = nil; 
     mapAnnotation.navaidType = [navaidTypesTempArray objectAtIndex:i]; 

     // Adding the annotation to the array that will be added to the map 
     [mapLocations addObject:mapAnnotation]; 
    } 

    // Adding annotations to the map 
    [self.mapView addAnnotations:mapLocations]; 
    } 
} 


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

    if([annotation isKindOfClass:[IGAMapAnnotation class]]) 
    { 
    IGAMapAnnotation *myLocation = (IGAMapAnnotation *) annotation; 
    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"IGAMapAnnotation"]; 

    if (annotationView == nil) 
     annotationView = myLocation.annotationView; 
    else 
     annotationView.annotation = annotation; 
    return annotationView; 
    } 
    else 
     return nil; 
} 

IGAMapAnnotation.m

@synthesize coordinate = _coordinate; 
@synthesize title = _title; 
@synthesize subtitle = _subtitle; 
@synthesize type = _type; 

// Tried to have this init method but was never able to make it work. Without it, the program crashes too!!!! 

-(id)initWithTitle:(NSString *)newTitle Type:(NSString *)type Location:(CLLocationCoordinate2D) newCoordinate 
{ 
    self = [super init]; 

    if (self) { 
     _title = newTitle; 
     _coordinate = newCoordinate; 
     _type = type; 
    } 

    return self; 
} 


-(MKAnnotationView *) annotationView { 
    MKAnnotationView *annotationView = [[MKAnnotationView alloc]initWithAnnotation:self reuseIdentifier:@"IGAMapAnnotation"]; 
    annotationView.enabled = YES; 
    annotationView.canShowCallout = YES; 
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 

    if ([self.type isEqual: @"A"] || [self.type isEqual: @"B"] || [self.type isEqual: @"C"]) 
    { 
    annotationView.image = [UIImage imageNamed:@"circle.png"]; 
    } 
    else if ([self.type isEqual: @"D"]) 
    { 
    annotationView.image = [UIImage imageNamed:@"triangle.png"]; 
    } 
    else if ([self.type isEqual: @"E"]) 
    { 
    annotationView.image = [UIImage imageNamed:@"square.png"]; 
    } 
    else 
    { 
    annotationView.image = [UIImage imageNamed:@"oval.png"]; 
    } 
    return annotationView; 
} 

@end 

입니다 :

그래서,이 코드입니다. 지금까지, 그 행동은 나에게 의미가 없습니다. 도움 주셔서 감사합니다.

+0

한 주 :이 '이동 [self.mapView의 removeAnnotations을 : mapLocations]'addAnnotation에 viewWillAppear에서의 상기 문제를 해결하지만, 그 이전 특수 핀 제거되지 . –

답변

3

주석보기 재사용 문제처럼 들립니다.

주석을 다시 표시 할 때 주석을 이전 주석의 이미지와 함께 다시 사용합니다. 뷰의 image 속성이 다른 주석에 대해 다시 사용될 때와 같이 업데이트되지 않습니다. viewForAnnotation 위임 방법에

,이 코드는 잘못된 보이는 다음 dequeue가 뷰 (예를 반환

MKAnnotationView *annotationView = [mapView dequeue... 
if (annotationView == nil) 
    annotationView = myLocation.annotationView; 
else 
    annotationView.annotation = annotation; 

경우 다른 유형의 주석에 대해 생성되었을 수도 이전에 만들어보기.), annotation 속성은 업데이트되지만 해당 image 속성은 업데이트되지 않습니다.

기존 코드는 dequeuenil 인 새로운 주석보기를 만들 때 image 속성 만 설정합니다. 지금

, 주석 보기 생성 및 image -setting 코드는 주석 모델 클래스 IGAMapAnnotation입니다. annotation 속성이 업데이트 될 때마다 image 속성을 자동으로 업데이트하는 사용자 지정 MKAnnotationView 클래스를 만드는 것이 좋습니다.

그러나 다른 대안은 viewForAnnotation 대리자 메서드 자체에 모든 논리를 넣고 IGAMapAnnotation 클래스에서 annotationView 메서드를 제거하는 것입니다. 업데이트 viewForAnnotation 위임 방법

예 :

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation 
{ 
    if (! [annotation isKindOfClass:[IGAMapAnnotation class]]) 
    { 
     //return default view if annotation is NOT of type IGAMapAnnotation... 
     return nil; 
    } 


    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"IGAMapAnnotation"]; 

    if (annotationView == nil) 
    { 
     annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"IGAMapAnnotation"]; 
     //these properties don't change per annotation 
     //so they can be set only when creating a new view... 
     annotationView.enabled = YES; 
     annotationView.canShowCallout = YES; 
     annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    } 
    else 
    { 
     annotationView.annotation = annotation; 
    } 


    //whether we are using a completely new view or a re-used view, 
    //set the view's image based on the current annotation... 

    IGAMapAnnotation *myLocation = (IGAMapAnnotation *) annotation; 
    if ([myLocation.type isEqual: @"A"] || [myLocation.type isEqual: @"B"] || [myLocation.type isEqual: @"C"]) 
    { 
     annotationView.image = [UIImage imageNamed:@"circle.png"]; 
    } 
    else if ([myLocation.type isEqual: @"D"]) 
    { 
     annotationView.image = [UIImage imageNamed:@"triangle.png"]; 
    } 
    else if ([myLocation.type isEqual: @"E"]) 
    { 
     annotationView.image = [UIImage imageNamed:@"square.png"]; 
    } 
    else 
    { 
     annotationView.image = [UIImage imageNamed:@"oval.png"]; 
    } 


    return annotationView; 
} 
+0

Anna. 나는 당신의 대체 방법을 사용했고 제대로 작동하고있는 것 같습니다.많이 고맙습니다 !!! –