objective c - Map view annotations with different pin colors -
i have array on 200 objects , trying perform loop through each of them.
each object have yes/no field , want display different coloured marker dependent on yes / no value.
from can see happening loop going through each object first , annotation added @ end each object .
since perform check within loop through array on yes no value when annotation added map, use yes/no value last object in array when goes plot all.
how can have marker different dependent on yes/no value each individual element?
my code is
for (i = 0; < [appdelegate.itemarray count]; i++) { item_details *tempobj = [appdelegate.itemarray objectatindex:i]; location.latitude = [tempobj.lat floatvalue]; location.longitude = [tempobj.lon floatvalue]; current_yesno = tempobj.yesno; mapviewannotation *newannotation = [[mapviewannotation alloc]initwithtitle:tempobj.name andcoordinate:location]; [self.mapview addannotation:newannotation]; [newannotation release]; } with annotation code follows
- (mkannotationview *) mapview:(mkmapview *)mapview viewforannotation:(id <mkannotation>) annotation{ mkpinannotationview *annview=[[mkpinannotationview alloc] initwithannotation:annotation reuseidentifier:@"currentloc"]; if(current_yesno == yes){ annview.pincolor = mkpinannotationcolorgreen; } else { annview.pincolor = mkpinannotationcolorred; } annview.animatesdrop=no; annview.canshowcallout = yes; annview.calloutoffset = cgpointmake(-5, 5); return annview; } and current_yesno declared in .h file.
the viewforannotation delegate method isn't called after addannotation , can called @ other times map view when needs view annotation (while code doing different).
so can't depend on value of ivar being in sync code outside delegate method.
instead, add yesno property custom mapviewannotation class, set when creating annotation , access value in viewforannotation through annotation parameter (ie. map view giving reference exact annotation object wants view for).
example:
mapviewannotation *newannotation = [[mapviewannotation alloc] init... newannotation.yesno = tempobj.yesno; // <-- set property in annotation [self.mapview addannotation:newannotation]; then in viewforannotation:
- (mkannotationview *) mapview:(mkmapview *)mapview viewforannotation:(id <mkannotation>) annotation { if (![annotation iskindofclass:[mapviewannotation class]]) { // return nil (default view) if annotation // custom class. return nil; } static nsstring *reuseid = @"currentloc"; mkpinannotationview *annview = (mkpinannotationview *)[mapview dequeuereusableannotationviewwithidentifier:reuseid]; if (annview == nil) { annview = [[mkpinannotationview alloc] initwithannotation:annotation reuseidentifier:reuseid]; annview.animatesdrop = no; annview.canshowcallout = yes; annview.calloutoffset = cgpointmake(-5, 5); } else { annview.annotation = annotation; } mapviewannotation *mvann = (mapviewannotation *)annotation; if (mvann.yesno) { annview.pincolor = mkpinannotationcolorgreen; } else { annview.pincolor = mkpinannotationcolorred; } return annview; }
Comments
Post a Comment