2011-05-27 48 views
1

我嘗試不同的圖像添加到不同的註解視圖中添加不同的圖像,以不同的註解視圖,換句話說,我希望有一個獨特的PIC對應於每一個獨特的針。下面是我想:在Xcode

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    NSLog(@"welcome into the map view annotation"); 

    // if it's the user location, just return nil. 
    if ([annotation isKindOfClass:[MKUserLocation class]]) 
     return nil; 

    // try to dequeue an existing pin view first 
    static NSString* AnnotationIdentifier = @"AnnotationIdentifier"; 
    MKPinAnnotationView* pinView = [[[MKPinAnnotationView alloc] 
            initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease]; 
    pinView.animatesDrop=YES; 
    pinView.canShowCallout=YES; 
    pinView.pinColor=MKPinAnnotationColorPurple; 


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

    if (CLLocationCoordinate2D == theCoordinate1) { 

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

    }else if(CLLocationCoordinate2D = theCoordinate2) { 
     UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Pierce.png"]]; 
     pinView.leftCalloutAccessoryView = profileIconView; 
     [profileIconView release]; 
    } 

我正在一個錯誤,我寫

if (CLLocationCoordinate2D == theCoordinate1) { 

行我不知道相當什麼是錯的,也不是我能想出其他方法標識個人註釋。任何幫助是極大的讚賞!!

+0

傑夫,歡迎來到SO!儘量避免在代碼粘貼中使用製表符 - 嘗試使用空格代替 - 在您的問題中使代碼格式正確。那麼幫助你更容易! – makdad 2011-05-27 02:07:57

回答

2

該行給出錯誤,因爲CLLocationCoordinate2D是一種結構,theCoordinate1是我假設的CLLocationCoordinate2D類型的變量。你無法比較兩者。

你正在嘗試做的是比較的視圖所被請求的座標theCoordinate1當前註釋的座標。要做到這一點,如果你一定要,你需要的是這樣的:

if ((annotation.coordinate.latitude == theCoordinate1.latitude) 
     && (annotation.coordinate.longitude == theCoordinate1.longitude)) { 

不過,我不建議比較浮點數這樣即使它「作品」的時候。如果一定要比較的座標,使用CLLocation的distanceFromLocation:方法,看看兩者之間的距離低於就像10.0米某個閾值。

另一種檢查註釋是否是您正在查找的註釋的方法是保留對註釋本身的引用(您傳遞給addAnnotation:方法的引用),然後您可以執行if (annotation == theAnnotation1)

如果您不想保留對註釋的引用,還可以檢查註釋的標題是否是您要查找的標題(if ([annotation.title isEqualToString:@"Jeff"]))。

最好的辦法是到自定義屬性(最好爲int)添加到自定義註釋類和檢查,在viewForAnnotation。


其他一些無關的事情:

+0

非常感謝 – jeff 2011-05-27 21:34:27