ios - Creating Custom Call out View -
i'm trying add view when click in pin, appears title , subtitle , not view did add.
here code
-(void) inserirmapa { appdelegate *appdelegate = (appdelegate *)[[uiapplication sharedapplication]delegate]; nsmutablearray *pins = [[nsmutablearray alloc]init]; //array de annotations for(int icnt = 0; icnt < [_listapins count]; icnt++) { pin *pin = (pin *) [_listapins objectatindex:icnt]; cllocationcoordinate2d start; start.latitude = pin.place.latitude; start.longitude = pin.place.longitude; mkcoordinateregion region = mkcoordinateregionmakewithdistance(start, 800, 800); [_mymapview setregion:[_mymapview regionthatfits:region] animated:yes]; // add annotation mkpointannotation *point = [[mkpointannotation alloc] init]; point.coordinate = start; point.title = @"where i?"; point.subtitle = @"i'm here!!!"; [_mymapview addannotation:point]; [pins addobject:point];//adiciona annotation no array [point release]; } self.myannotations= [nsarray arraywitharray:pins]; //diz que o array myannotations é igual ao array estacionamentos(que contem annotations [pins release]; [self configurarzoomdomapacomlatitude:appdelegate.latitude elongitude:appdelegate.longitude]; //configura o zoom inicial mapa [_mymapview addannotations:_myannotations]; [_viewloading sethidden:yes]; }
and here add custom view. put breakpoint here , function called, when click in pin, appearas title , subtitle..ignoring view added
- (mkannotationview *)mapview:(mkmapview *)mapview viewforannotation:(id <mkannotation>)annotation { mkannotationview* annotationview = nil; //your code uiview *vw = [[uiview alloc] initwithframe:cgrectmake(0, 0, 100, 100)]; vw.backgroundcolor = [uicolor redcolor]; uilabel *label = [[uilabel alloc] initwithframe:cgrectmake(5, 5, 50, 50)]; label.numberoflines = 4; label.text = @"hello\nhow you\nfine"; [vw addsubview:label]; annotationview.leftcalloutaccessoryview = vw; return annotationview; }
in viewforannotation
, code declares , initializes annotationview
nil
, never instantiates (alloc+init).
therefore, delegate method returns nil
tells map view "create default view annotation". default view annotation other user location red pin callout shows title , subtitle.
however, if alloc+init plain mkannotationview
fix it, annotations not visible since default mkannotationview
has no content. need set image
or add subview.
instead, alloc+init mkpinannotationview
default show red pin.
should implement annotation view re-use calling dequeuereusableannotationviewwithidentifier
improve performance when there lots of annotations:
- (mkannotationview *)mapview:(mkmapview *)mapview viewforannotation:(id <mkannotation>)annotation { static nsstring *reuseid = @"ann"; mkpinannotationview *annotationview = (mkpinannotationview *)[mapview dequeuereusableannotationviewwithidentifier:reuseid]; if (annotationview == nil) { annotationview = [[mkpinannotationview alloc] initwithannotation:annotation reuseidentifier:@"test"]; annotationview.canshowcallout = yes; //set yes, default no //your code uiview *vw = [[uiview alloc] initwithframe:cgrectmake(0, 0, 100, 100)]; vw.backgroundcolor = [uicolor redcolor]; uilabel *label = [[uilabel alloc] initwithframe:cgrectmake(5, 5, 50, 50)]; label.numberoflines = 4; label.text = @"hello\nhow you\nfine"; [vw addsubview:label]; annotationview.leftcalloutaccessoryview = vw; } else { //update view's annotation reference //because re-using view may have //been used annotation... annotationview.annotation = annotation; } return annotationview; }
should fix custom leftcalloutaccessoryview
not appearing.
however, note documentation says leftcalloutaccessoryview
(and rightcalloutaccessoryview
):
the height of view should 32 pixels or less.
the custom view , label code creating both more 32 pixels high.
unrelated comment regarding loop adds annotations:
not need call setregion
adding each annotation.
calling setregion
tells map view display indicated region.
not needed add annotation (the map doesn't have displaying coordinate adding annotation at).
Comments
Post a Comment