我使用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
誰能給我一個提示我如何顯示基於布爾值的綠色和紅色引腳?
在此先感謝!
在MapViewAnnotation類中,這個布爾值在哪裏存儲?然後可以在viewForAnnotation委託方法中訪問該布爾值,並相應地設置pinColor。不相關,但是您當前的viewForAnnotation方法也忽略了dequeueReusableAnnotationViewWithIdentifier的結果並始終創建一個新視圖。 – Anna
謝謝安娜!對不起,我一直在困擾其他工作。布爾值存儲在iniWithObects ..我可以得到它與下面的代碼塊:NSNumber * statusBoolean = [NSNumber numberWithBool:YES]; statusBoolean = [_object objectForKey:@「status」]; NSLog(@「%@」,statusBoolean);那麼我如何在vieForAnnotation委託方法中使用它? – MarcJohnson
我認爲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