2010-12-01 12 views
7

マップビューでは、現在のユーザーの場所を表示しています。ピンをクリックして "Current Location"を表示します。私はそれを "My Current Location"に変更したいと思います。どうすれば変更できますか? また、タイマーで現在のユーザーの位置ピンの色を変更したいとします。緑、紫、赤の間で色を変えなければならないようなものがあります。それは可能ですか?iPadマップキット - 現在の場所のタイトルを変更する

私はマップキットのショーのデフォルトの場所を使用して、以下のように注釈ピンの色を操作しています:

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation{ 
static NSString *AnnotationViewID = @"annotationViewID"; 
SolarAnnotationView* annotationView = (SolarAnnotationView*)[map dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID]; 
if(annotationView == nil) 
{ 
    if([self CLLocationCoordinate2DEquals:mapView.userLocation.location.coordinate withSecondCoordinate:[annotation coordinate]]) //Show current location with green pin 
    { 
     annotationView = [[SolarAnnotationView alloc] initWithAnnotation:annotation]; 
     annotationView.delegate = self; 
     [annotationView setPinColor:MKPinAnnotationColorGreen]; 
    } 
    else 
    { 
     annotationView = [[SolarAnnotationView alloc] initWithAnnotation:annotation]; 
     annotationView.delegate = self; 
    } 
} 

return annotationView; 

}

- (BOOL) CLLocationCoordinate2DEquals:(const CLLocationCoordinate2D)lhs withSecondCoordinate:(const CLLocationCoordinate2D) rhs{ 
const CLLocationDegrees DELTA = 0.001; 
return fabs(lhs.latitude - rhs.latitude) <= DELTA && fabs(lhs.longitude - rhs.longitude) <= DELTA; 

}

+0

表示:

は、このようなviewForAnnotation方法を変更します。マップビューのshowsUserLocationプロパティを使用してデフォルトの青い点を得るかカスタムピンを作成していますか?アノテーションとviewForAnnotationメソッドの追加方法を示します。 – Anna

答えて

20

あなたはマップビューショーを聞かせている場合ユーザーの場所(青い点)のデフォルトのアノテーションビューですが、これは実装が簡単です(クールなアニメーションのズームサークルで素敵な青い点が得られます)。

青い点の代わりにピン画像を使用してユーザーの場所を表示する必要がある場合は、もう少し作業が必要です。

まず、ブルーのドットを使用して簡単な方法:

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation{ 
    if ([annotation isKindOfClass:[MKUserLocation class]]) 
    { 
     ((MKUserLocation *)annotation).title = @"My Current Location"; 
     return nil; //return nil to use default blue dot view 
    } 

    //Your existing code for viewForAnnotation here (with some corrections)... 
    static NSString *AnnotationViewID = @"annotationViewID"; 
    SolarAnnotationView* annotationView = (SolarAnnotationView*)[map dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID]; 
    if(annotationView == nil) 
    { 
     { 
      annotationView = [[[SolarAnnotationView alloc] initWithAnnotation:annotation] autorelease]; 
      //added autorelease above to avoid memory leak 
      annotationView.delegate = self; 
     } 
    } 

    //update annotation in view in case we are re-using a view 
    annotationView.annotation = annotation; 

    return annotationView; 
} 


あなたの代わりに、ユーザーの場所のカスタム注釈ビューを使用する場合は、カスタムビューにコードを変更するピンの色を置く必要があります。定期的に色を変更する方法の1つは、performSelector:withObject:afterDelay:を使用することです。

-(void)startChangingPinColor 
{ 
    switch (self.pinColor) { 
     case MKPinAnnotationColorRed: 
      self.pinColor = MKPinAnnotationColorGreen; 
      break; 
     case MKPinAnnotationColorGreen: 
      self.pinColor = MKPinAnnotationColorPurple; 
      break; 
     default: 
      self.pinColor = MKPinAnnotationColorRed; 
      break; 
    } 
    [self performSelector:@selector(startChangingPinColor) withObject:nil afterDelay:1.0]; 
} 

-(void)stopChangingPinColor 
{ 
    [NSObject cancelPreviousPerformRequestsWithTarget:self]; 
} 

またSolarAnnotationView.hファイルにメソッドのヘッダを追加:SolarAnnotationView.mでは、これら2つのメソッドを追加します。あなたが現在の場所を示す方法

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation{ 
    static NSString *AnnotationViewID = @"annotationViewID"; 
    SolarAnnotationView* annotationView = (SolarAnnotationView*)[map dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID]; 
    if(annotationView == nil) 
    { 
     { 
      annotationView = [[[SolarAnnotationView alloc] initWithAnnotation:annotation] autorelease]; 
      annotationView.delegate = self; 
     } 
    } 

    //Update annotation in view in case we are re-using a view... 
    annotationView.annotation = annotation; 

    //Stop pin color changing in case we are re-using a view that has it on 
    //and this annotation is not user location... 
    [annotationView stopChangingPinColor]; 

    if([self CLLocationCoordinate2DEquals:mapView.userLocation.location.coordinate withSecondCoordinate:[annotation coordinate]]) //Show current location with green pin 
    { 
     [annotationView setPinColor:MKPinAnnotationColorGreen]; 
     annotationView.canShowCallout = YES; 
     ((MKPointAnnotation *)annotation).title = @"My Current Location"; 
     [annotationView startChangingPinColor]; 
    } 

    return annotationView; 
} 
+1

ありがとうございました。 – Satyam

関連する問題