2013-10-22 74 views
0

我使用Parse.com作爲後端,並且我想在我的地圖上顯示Geopoints。 每個Geopoint也與數據庫布爾字段true或false連接。MKAnnotationView - 設置兩種不同的針顏色

如何顯示「真」Gepoint和「假」Geopoint的紅色引腳的綠色針腳顏色?

下面是MapViewController.m

@property (weak, nonatomic) IBOutlet MKMapView *mapView; 

我必須執行對parse.com數據庫查詢返回我的所有位置數據的功能代碼。它在viewDidLoad方法中調用。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self getAllStations]; 
} 

然後我這樣設置annotationView:

#pragma mark - MapViewDelegate 

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)geoPointAnnotation { 
    static NSString *MapViewAnnotationIdentifier = @"Places"; 

    MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:MapViewAnnotationIdentifier]; 


    if (geoPointAnnotation == mapView.userLocation) { 
     return nil; 

    } else { 

     annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:geoPointAnnotation reuseIdentifier:MapViewAnnotationIdentifier]; 
     annotationView.pinColor = MKPinAnnotationColorGreen; 
     annotationView.canShowCallout = YES; 
     annotationView.draggable = YES; 
     annotationView.animatesDrop = YES; 
    } 

    return annotationView; 
} 

這裏是代碼爲MapViewAnnotation.m(對象):

#import "MapViewAnnotation.h" 
#import "Parse/Parse.h" 

@interface MapViewAnnotation() 

@property (nonatomic, strong) PFObject *object; 

@end 


@implementation MapViewAnnotation 


#pragma mark - Initialization 

- (id)initWithObject:(PFObject *)aObject { 
    self = [super init]; 
    if (self) { 
     _object = aObject; 

     PFGeoPoint *geoPoint = self.object[@"location"]; 
     [self setGeoPoint:geoPoint];  } 
    return self; 
} 


- (void)setGeoPoint:(PFGeoPoint *)geoPoint { 
    _coordinate = CLLocationCoordinate2DMake(geoPoint.latitude, geoPoint.longitude); 

     NSString *streetName = self.object[@"adress"]; 

    _title = [NSString stringWithFormat:@"%@", [_object objectForKey:@"name"]]; 


    [PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *currentLocationGeoPoint, NSError *error) { //Get current Location 
     if (!error) { 

      PFGeoPoint *distanceGeoPoint = [_object objectForKey:@"location"]; 

      double distanceDouble = [currentLocationGeoPoint distanceInKilometersTo:distanceGeoPoint]; 
      //NSLog(@"Distance: %.1f",distanceDouble); 

      _subtitle = [NSString stringWithFormat:@"%@ - Distance: %.1f km", streetName, distanceDouble]; 
     } 
    }]; 
} 
@end 

誰能給我一個提示我如何顯示基於布爾值的綠色和紅色引腳?

在此先感謝!

+0

在MapViewAnnotation類中,這個布爾值在哪裏存儲?然後可以在viewForAnnotation委託方法中訪問該布爾值,並相應地設置pinColor。不相關,但是您當前的viewForAnnotation方法也忽略了dequeueReusableAnnotationViewWithIdentifier的結果並始終創建一個新視圖。 – Anna

+0

謝謝安娜!對不起,我一直在困擾其他工作。布爾值存儲在iniWithObects ..我可以得到它與下面的代碼塊:NSNumber * statusBoolean = [NSNumber numberWithBool:YES]; statusBoolean = [_object objectForKey:@「status」]; NSLog(@「%@」,statusBoolean);那麼我如何在vieForAnnotation委託方法中使用它? – MarcJohnson

+0

我認爲MapViewAnnotation是您的類實現MKAnnotation?你在該課堂中擁有「物體」作爲財產。通過將'annotation'參數轉換爲MapViewAnnotation,可以在'viewForAnnotation'中訪問該屬性(以及其中的status bool)。請參閱http:// stackoverflow。com/questions/10898122/map-view-annotations-with-different-pin-colors和http://stackoverflow.com/questions/5939223/store-data-in-mkannotation舉例。嘗試一下,如果有問題,用你試過的代碼更新你的問題。 – Anna

回答

-1

任何人都可以給我一個提示如何顯示基於布爾值的綠色和紅色的引腳?

BOOL colorRed = YES; 

if (colorRed) annotationView.pinColor = MKPinAnnotationColorRed; 
else annotationView.pinColor = MKPinAnnotationColorGreen; 

那是什麼意思?

+0

你好jbat。是的,這就是我的意思。但是我面臨的問題是我不知道如何檢查每個註釋項目是否爲真。我可以使用NSNumber * statusBoolean = [NSNumber numberWithBool:YES]來獲取「initWithObjects」中的布爾數據; statusBoolean = [_object objectForKey:@「status」]; NSLog(@「%@」,statusBoolean);那麼我怎麼能在pinAnnotationView中使用這些數據呢? – MarcJohnson

0

您可以自定義針標註視圖,

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

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



    MKAnnotationView *flagAnnotationView = 
    [self.mapView dequeueReusableAnnotationViewWithIdentifier:SFAnnotationIdentifier]; 
    if (flagAnnotationView == nil) 
    { 
     MKPinAnnotationView *customPinView = [[MKPinAnnotationView alloc] 
               initWithAnnotation:annotation reuseIdentifier:SFAnnotationIdentifier]; 

    if(annotation.coordinate.latitude==42.0000 && annotation.coordinate.longitude==-87.65000) 
    //you have to keep the latitude and longitude of the pins to which you can set the colour of the pin according to that latlong 
     customPinView.pinColor = MKPinAnnotationColorGreen; 
    else 
     customPinView.pinColor = MKPinAnnotationColorRed; 
     customPinView.animatesDrop = YES; 
     customPinView.canShowCallout = YES; 
     return customPinView; 
    } 
     else 
     { 
     flagAnnotationView.annotation = annotation; 
     } 
     return flagAnnotationView; 

     return nil; 
    } 
+0

我不建議使用直接相等來比較浮點數。使用非浮點屬性(也直接附加到註釋對象),如OP請求的字符串,整數或布爾值是更可取的。此外,此示例代碼不能正確處理視圖重用(在flagAnnotationView不爲零的情況下pinColor未設置 - 第二個else內部)。 – Anna

0
在annotation.h文件

聲明的NSString如下

@property (strong , nonatomic)NSString *pinColour; 

,並在FPGA實現文件做以下檢查顏色

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

//other code above 
if ([[annotation pinColour] isEqualToString:@"Red"]) { 
     annotationView.pinColor = MKPinAnnotationColorRed; 
    }else{ 
     annotationView.pinColor = MKPinAnnotationColorGreen; 
    } 
} 

和裏面的for循環的解析,標記有關與通過枯萎顏色

annotation.pinColour = @"Red"; 
相關問題