ios - Refreshing annotations mix photos -
first time view controller load, download data web service, use details add annotations. when tap button download again data , put in on again, wrong photos each , every time. tried clear cache , doesn't help, tried lot of ways. ideas?
edit:
- (void)addcustomerstomap { marker = nil; profilepics = [[nsmutablearray alloc] init]; if ([self.usersnearme iskindofclass:[nsdictionary class]]) { nsdictionary *singleuser = (nsdictionary*)self.usersnearme; self.usersnearme = [[nsarray alloc] initwithobjects:singleuser, nil]; } if ([self.usersnearme iskindofclass:[nsarray class]]) { (int = 0; < self.usersnearme.count; i++) { nsdictionary *dict = self.usersnearme[i]; float lat = [[dict objectforkey:@"lat"] floatvalue]; float lon = [[dict objectforkey:@"lon"] floatvalue]; nsstring *name = [dict objectforkey:@"name"]; nsstring *time = [dict objectforkey:@"time"]; nsstring *picurl = [dict objectforkey:@"pictureurl"]; [profilepics addobject:picurl]; cllocationcoordinate2d mloc = cllocationcoordinate2dmake(lat, lon); if ([picurl isequaltostring:@"empty"]) { marker = [[customermarker alloc] initwithlocation:mloc title:name andsubtitle:time andurlstring:nil andimageperson:[uiimage imagenamed:@"user"]]; } else if ([picurl isequaltostring:@"system"]) { marker = [[customermarker alloc] initwithlocation:mloc title:name andsubtitle:time andurlstring:nil andimageperson:[uiimage imagenamed:@"police_icon"]]; } else { nsstring *completeurl = @""; completeurl = [completeurl stringbyappendingstring:kprofile_image_url]; completeurl = [completeurl stringbyappendingstring:@"/2/"]; completeurl = [completeurl stringbyappendingstring:picurl]; completeurl = [completeurl stringbyappendingstring:@".png"]; nsurl *url = [nsurl urlwithstring:completeurl]; nsurlrequest *urlrequest = [nsurlrequest requestwithurl:url]; [nsurlconnection sendasynchronousrequest:urlrequest queue:[nsoperationqueue mainqueue] completionhandler:^(nsurlresponse *response, nsdata *responsedata, nserror *error) { if (responsedata) { nsstring *imagename = completeurl.lastpathcomponent; nsstring *imagepath = [[self documentsdirectorypath] stringbyappendingstring:imagename]; nslog(@"imagepath: %@", imagepath); [responsedata writetofile:imagepath atomically:yes]; marker = [[customermarker alloc] initwithlocation:mloc title:name andsubtitle:time andurlstring:nil andimageperson:[uiimage imagewithcontentsoffile:imagepath]]; dispatch_async(dispatch_get_main_queue(), ^{ [self.btnrefreshusers setimage:[uiimage imagewithcontentsoffile:imagepath] forstate:uicontrolstatenormal]; [self.mapview viewforannotation:marker]; [self.mapview addannotation:marker]; }); } }]; } } } self.btnrefreshusers.userinteractionenabled = yes; } - (mkannotationview *)mapview:(mkmapview *)mapview viewforannotation:(id<mkannotation>)annotation { dispatch_async(dispatch_get_main_queue(), ^{ if ([annotation iskindofclass:[mkuserlocation class]]) { [self.mapview selectannotation:((mkuserlocation*) annotation) animated:yes]; } }); if ([annotation iskindofclass:[mkuserlocation class]]) { ((mkuserlocation*) annotation).title = nslocalizedstring(@"current_location", nil); return nil; } if ([annotation iskindofclass:[customermarker class]]) { mkannotationview *pinview = nil; static nsstring *customermarkerpinid = @"com.idm.customer"; pinview = (mkannotationview*)[mapview dequeuereusableannotationviewwithidentifier:customermarkerpinid]; if (!pinview) { pinview = [[mkannotationview alloc] initwithannotation:annotation reuseidentifier:customermarkerpinid]; } pinview.canshowcallout = yes; pinview.backgroundcolor = [uicolor clearcolor]; customermarker *customermarker = (customermarker*)annotation; pinview.image = customermarker.imageperson; cgrect pinviewframe = cgrectzero; pinviewframe.origin.x = pinview.frame.origin.x; pinviewframe.origin.y = pinview.frame.origin.y; pinviewframe.size.width = 40.0f; pinviewframe.size.height = 40.0f; pinview.frame = pinviewframe; ////////// animate pin drop ////////// cgrect endframe = pinview.frame; pinview.frame = cgrectoffset(pinview.frame, 0, -230); [uiview beginanimations:nil context:nil]; [uiview setanimationduration:0.45f]; [uiview setanimationcurve:uiviewanimationcurveeaseinout]; pinview.frame = endframe; [uiview commitanimations]; /* cgrect endframe = pinview.frame; pinview.frame = cgrectoffset(pinview.frame, 0, -230); [uiview animatewithduration:0.45f delay:0 options:uiviewanimationoptionshowhidetransitionviews animations:^{ [uiview setanimationcurve:uiviewanimationcurveeaseinout]; } completion:^(bool finished) { if (finished) { pinview.frame = endframe; } }]; */ return pinview; } return nil; }
there 2 major problems here. first you're reusing pinview not setting attributes according annotation trying draw. you're correct dequeue reusable annotationview, if reuse 1 or don't need set image 1 want draw right otherwise pinview's image 1 previously. e.g.
if ([annotation iskindofclass:[customermarker class]]) { mkannotationview *pinview = nil;//create new handle, don't risk having 1 global 1 static nsstring *customermarkerpinid = @"com.idm.customer"; pinview = (mkannotationview*)[mapview dequeuereusableannotationviewwithidentifier:customermarkerpinid]; if (!pinview) { pinview = [[mkannotationview alloc] initwithannotation:annotation reuseidentifier:customermarkerpinid]; //since pinviews can show callouts need set //when creating new one, not when reusing existing 1 // have set pinview.canshowcallout = yes; } pinview.backgroundcolor = [uicolor colorwithpatternimage:[uiimage imagenamed:@"map_pic"]]; ... return pinview; } //[self.mapview setneedsdisplay]; <--- don't return nil;
secondly you're choosing image each pin based on profilepicscounter
changed while you're adding images mapview can , ask views in order feels like not order add them. need respond viewforannotation
calculating view based on annotation gives you. clue in name. line:
nsstring *urlstring = profilepics[profilepicscounter];
needs
myannotationclass *myanno = (myannotationclass)annotation nsstring *urlstring = myanno.urlstring;
essentially you've got store info needs within annotation.
also, annotations part of removeallmarkers
function can shortened 1 line
[self.mapview removeannotations:self.mapview.annotations];
and you're setting backgroundcolor on pinviews twice
Comments
Post a Comment