2010-12-01 25 views
7

Harita görünümünde, geçerli kullanıcı konumunu gösteriyorum. Pin üzerinde "Mevcut Konumu" göstererek üzerine tıklayın. Onu "Geçerli Konumum" olarak değiştirmek istiyorum. Bunu nasıl değiştirebilirim? Ayrıca mevcut kullanıcı konumu pin rengini bir zamanlayıcıda değiştirmek istiyorum. Her saniye gibi bir şey yeşil, mor ve kırmızı arasındaki rengini değiştirmelidir. Bunu yapmak mümkün mü?iPad Mapkit - "Geçerli Konum" başlığını değiştir

Ben haritası kitinin gösteri varsayılan konumu kullanarak ve daha sonra aşağıdaki gibi açıklama pimi rengini manipüle ediyorum:

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

göster:

Sonra böyle viewForAnnotation yöntemini değiştirmek. Varsayılan mavi nokta almak veya özel bir iğne oluşturmak için harita görünümünün showUserLocation özelliğini kullanıyor musunuz? Ek açıklama ve viewForAnnotation yöntemini nasıl eklediğinizi gösterin. – Anna

cevap

20

Eğer harita görünümü gösterisini izin verirseniz kullanıcı konumu için varsayılan ek açıklama görünümü (mavi nokta), bunu uygulamak daha kolaydır (ve serin animasyonlu yakınlaştırma dairesiyle güzel bir mavi nokta elde edersiniz).

Kullanıcı konumunu mavi nokta yerine iğne görüntüsünü kullanarak göstermeniz gerekiyorsa, daha fazla çalışmaya gerek vardır.

Birincisi, mavi nokta kullanarak basit yol: yerine kullanıcı konumu için özel açıklama görünümünü kullanmak istiyorsanız

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


, özel görünümünde pim renk değiştirme kodu koymalıyız . Rengi periyodik olarak değiştirmenin bir yolu performSelector: withObject: afterDelay: kullanıyor. SolarAnnotationView.m, bu iki yöntem ekleyin:

-(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]; 
} 

Ayrıca SolarAnnotationView.h dosyasına yöntem başlıklarını ekleyin. Eğer mevcut konumunu gösteren nasıl

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

Bilgi için çok teşekkür ederim. – Satyam

İlgili konular