2012-08-31 3 views
0

mapView에서 어노테이션과 관련된 문제가 있습니다. 내 요구 사항을 한 눈에 볼 수 있습니다.mkMapView에서 세그먼트를 바꿀 때 핀 색상을 변경할 수 없습니다.

사용자에게 회의 위치를 ​​선택하도록 선택하고 싶습니다.

두 가지 옵션이 있습니다.

1) 나는 그가 끌어 어디서나 자신이 원하는 핀을 놓을 수 있습니다 데이터

또는

2)에 의해 근처의 목록을 제공한다!

그 때문에 하나의 세그먼트가 생성되었습니다. 데이터 근처의 첫 번째 인덱스 및 핀을 놓을 때의 두 번째 인덱스입니다. 첫 번째 옵션의

(는 "근처에 의해") 나는 판매자, 판매자와 구매자 사이의 역할의 위치와 중간 점의 위치에서 데이터 근처에 가져올 필요가있다. 그래서 Google api를 호출하고 위도와 경도를 세 번 통과시켜 데이터를 가져옵니다. 처음으로 데이터를 얻을 때 아무런 문제가 없습니다. 내 배열은 모든 데이터 (3 개의 응답 포함)로 채우고 핀 색상도 요구 사항에 따라 변경됩니다. 내가 드롭 핀을 클릭하면

역할 (레드 컬러) 판매자 (퍼플) 중간 지점 (녹색)

이제 모든 데이터가 배열에서 제거하고 하나의 핀이지도에 삭제됩니다.

지금까지 제대로 작동합니다.

하지만 다시 "가까운 곳"을 클릭하면 문제가 시작됩니다! 필자가 원하는대로 데이터를 제공하지만 핀 색상이 유지되지 않는 것은 틀림 없습니다.

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

    if ([segmentND selectedSegmentIndex]==0) { 

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

     static NSString* BridgeAnnotationIdentifier = @"bridgeAnnotationIdentifier"; 
     MKPinAnnotationView* pinView = (MKPinAnnotationView *) 
     [myMapView dequeueReusableAnnotationViewWithIdentifier:BridgeAnnotationIdentifier]; 
     if (!pinView) 
     { 
      MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc] 
                initWithAnnotation:annotation reuseIdentifier:BridgeAnnotationIdentifier] autorelease]; 

      switch (self.pinColor) { 
       case 0: 
       { 
        customPinView.pinColor = MKPinAnnotationColorPurple; 
       } 
        break; 
       case 1: 
       { 
        customPinView.pinColor = MKPinAnnotationColorRed; 
       } 
        break; 
       case 2: 
       { 
        customPinView.pinColor = MKPinAnnotationColorGreen; 
       } 
        break; 
       default: 
        break; 
      } 
      customPinView.canShowCallout = YES; 

      UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 


      [rightButton addTarget:self 
          action:@selector(showDetails:) 
        forControlEvents:UIControlEventTouchUpInside]; 
      customPinView.rightCalloutAccessoryView = rightButton; 

      return customPinView; 
     } 
     else 
     { 
      pinView.annotation = annotation; 
     } 
     return pinView; 
    } 
    else { 

     // Code of dragging dropping pin. It works Fine.s 
    } 
} 

자세한 내용은 image을 첨부하고 있습니다.

해결 방법이나 다른 방법으로 구현하십시오. 핀 색상은 판매자 구매자와 중간 점을 구별하는 데 필수적임을 기억하십시오!

+0

컨트롤이 내부에 들어가거나 브레이크 포인트를 사용하지 않는 스위치 조건을 – Rajneesh071

+0

self.pinColor는 어떤 값을 유지합니까? – Neo

답변

0

재사용 가능한 핀이없는 경우 코드 부분에 핀 컬러를 설정합니다.

if (!pinView) 
    .... 
    customPinView.pinColor = MKPinAnnotationColorPurple; 
    .... 
} 

viewForAnnotation이 호출되고 재사용 가능한 핀이 발견되면 사용됩니다. 틀린 색깔 핀이 가지고가는 곳에있다.

else 
{ 
    pinView.annotation = annotation; 
} 

부분에 pincolor를 설정하고 그것을 잘 작동합니다.

0

는 내가 작은 코드 & MKMapView 대리자를 수정 한 것

else 
    { 
     pinView.annotation = annotation; 
    } 
    return pinView; 

수정이

다음
else 
    { 
     pinView.annotation = annotation;   
     return pinView; 
    } 
0

여기에 잘못된 거 같아요. 예전에 pin 색을 if 내부로 변경하여 처음으로 MKMapView이로드 될 때 한 번만 전화를 걸었습니다.

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation 
{    
    if ([annotation isKindOfClass:[MKUserLocation class]]) 
     return nil; 

    static NSString* BridgeAnnotationIdentifier = @"bridgeAnnotationIdentifier"; 
    MKPinAnnotationView* pinView = (MKPinAnnotationView *) 
    [mapView dequeueReusableAnnotationViewWithIdentifier:BridgeAnnotationIdentifier]; 
    if (!pinView) 
    { 
     pinView = [[[MKPinAnnotationView alloc] 
              initWithAnnotation:annotation reuseIdentifier:BridgeAnnotationIdentifier] autorelease]; 
    } 
    else 
    { 
     pinView.annotation = annotation; 
    } 

    switch (self.pinColor) { 
     case 0: 
     { 
      pinView.pinColor = MKPinAnnotationColorPurple; 

     } 
      break; 
     case 1: 
     { 
      pinView.pinColor = MKPinAnnotationColorRed; 
     } 
      break; 
     case 2: 
     { 
      pinView.pinColor = MKPinAnnotationColorGreen; 
     } 
      break; 
     default: 
      break; 

    pinView.canShowCallout = YES; 

    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 


    [rightButton addTarget:self 
        action:@selector(showDetails:) 
      forControlEvents:UIControlEventTouchUpInside]; 
    pinView.rightCalloutAccessoryView = rightButton; 

    return pinView; 
} 

    return pinView; 
} 

P. 제 첫 번째 확인을 위해 처음으로 UISegment 조건을 제거했습니다. 구현할 때 그대로 추가하십시오.

+0

그것은 작동합니다! 하지만 내지도를 확대/축소 할 때 같은 문제가 발생합니다! – Chintan

+0

확대 및 축소 할 때 어떤 일이 발생 했습니까? – Hemang

+0

이제 모든 핀을 무작위로 하나의 색상 (보라색, 빨간색 또는 녹색)으로 만듭니다. – Chintan

1

현재 접근법의 문제는 맵에서보기가 필요한 주석에 따라 self.pinColor가 변경되지 않는다는 것입니다. 언제든지 viewForAnnotation을 호출 할 수 있습니다. 어쩌면지도가 스크롤되어 하나의 핀이 다시 볼 수있게되었을 것입니다. 어쩌면 앱이 백그라운드에 놓여져 사용자가 다시 볼 수있게되었을 수도 있습니다. 주석을 분석해야하는 이유는 뷰에서 사용할 핀 색상을 결정하기 위해 전달하는 것입니다. 특수 효과에 어떤 객체를 사용하고 있습니까? 그것이 HSAnno이고 pinColor라는 속성을 갖고 있다면 switch 문 대신이 작업을 수행 할 것입니다.

HSAnno* currentAnno = (HSAnno *)annotation; 
pinView.pinColor = currentAnno.pinColor; 

viewForAnnotation이 항상 옳은 색 핀을 반환 다시 그려야 할 필요가 무엇 주석에 상관없이 그런 식으로.

+0

내 프레임 워크에 이러한 클래스가 없습니다! 이 제 3 자 코드입니까? – Chintan

+0

HSAnno 클래스는 예제로 만들었습니다 (Hemang Shah의 답변이 텍스트 상자 위에 있었고 귀하의 이름 대신 이니셜을 사용했습니다). 수업은 실제로 존재하지 않습니다. 수업을 작성하는 것은 학생의 몫입니다. MKAnnotationProtocol을 구현하고 속성 (pinColor 또는 기타라고도 함)을 추가해야합니다. 그런 다음 viewForAnnotation이 호출되면 클래스에 일반 주석 객체를 캐스팅하고 (userPin이 아닌지 확인한 후) 속성의 값에 따라 핀의 색상을 설정합니다. – Craig

관련 문제