iphone - Free hand Draw polyline overlay in ios -
i'm trying trace route free hand on mkmapview
using overlays (mkoverlay
).
each time when move finger extend polyline last coordinate new coordinate,all working fine except when extending polyline overlay whole overlay blinking in device(only sometimes),so can,t trace problem.
the code have tried given below.
- (void)viewdidload { j=0; coords1 = malloc(2* sizeof(cllocationcoordinate2d)); coordinatearray=[[nsmutablearray alloc]init]; uipangesturerecognizer *gesturerecogonized = [[uipangesturerecognizer alloc] initwithtarget:self action:@selector(gesturedetacted:)]; [self.mymapview addgesturerecognizer:gesturerecogonized]; } - (void)gesturedetacted:(uipangesturerecognizer *)recognizer { if(uigesturerecognizerstatebegan==recognizer.state) { cgpoint point = [recognizer locationinview:self.mymapview]; cllocationcoordinate2d tappoint = [self.mymapview convertpoint:point tocoordinatefromview:self.view]; cllocation *curlocation = [[cllocation alloc] initwithlatitude:tappoint.latitude longitude:tappoint.longitude]; [coordinatearray addobject:curlocation]; } coords1[0]=[[coordinatearray objectatindex:j] coordinate]; if(uigesturerecognizerstatechanged==recognizer.state) { j++; cgpoint point = [recognizer locationinview:self.mymapview]; cllocationcoordinate2d tappoint = [self.mymapview convertpoint:point tocoordinatefromview:self.view]; cllocation *curlocation = [[cllocation alloc] initwithlatitude:tappoint.latitude longitude:tappoint.longitude]; [coordinatearray addobject:curlocation]; coords1[1]=cllocationcoordinate2dmake(tappoint.latitude,tappoint.longitude); polyline = [mkpolyline polylinewithcoordinates:coords1 count:2]; [self.mymapview addoverlay:polyline]; } }
in overlay delegate
- (mkoverlayview *)mapview:(mkmapview *)mapview viewforoverlay:(id <mkoverlay>)overlay { if([overlay iskindofclass:[mkpolyline class]]){ mkpolylineview *polylineview = [[mkpolylineview alloc] initwithpolyline:overlay]; polylineview.strokecolor = [uicolor orangecolor]; polylineview.linewidth = 20; polylineview.fillcolor=[[uicolor orangecolor] colorwithalphacomponent:.1]; return polylineview; } }
can know why flickering or blinking effect coming , how remove it.
thanks in advance.
rather adding hundreds of small views (which computationally intensive), rather remove polyline overlay , add new 1 points on map in @ each change in pan recognizer (for smoother effect, can first add new 1 , remove old one). use coordinatearray create mkpolyline overlay contains of points, rather last 2 points. like:
[coordinatearray addobject:curlocation];; cllocationcoordinate2d* coordarray = malloc(sizeof(cllocationcoordinate2d)*[coordinatearray count]); memcpy(coordarray, self.coordinates, sizeof(cllocationcoordinate2d)*([coordinatearray count]-1)); coordarray[[coordinatearray count]-1] = curlocation; free(self.coordinates); self.coordinates = coordarray; mkpolyline *polyline = [mkpolyline polylinewithcoordinates:coordarray count:[coordinatearray count]]; mkpolyline *old = [[self.mapview overlays] lastobject]; [self.mapview addoverlay:polyline]; [self.mapview removeoverlay:old];
where self.coordinate property of type cllocationcoordinate2d*, way can memcpy existing array new 1 (memcpy efficent) , need add last point array, rather having loop through each point of nsarray *coordinatearray
.
you have change if(uigesturerecognizerstatebegan==recognizer.state)
part, add in self.coordinates first tapped point. like:
self.coordinates = malloc(sizeof(cllocationcoordinate2d)); self.coordinates[0] = curlocation;
edit: think problem map overlays draw different discrete values of zoom levels. @ zoom level appear overlay first draws @ bigger zoom level, , @ smaller zoom level (in fact drawing on drawn overlay). when try show animation such drawing user panning gesture @ zoom level, why overlay keeps flickering. possible solution use transparent view put on top of map view, drawing performed while user keeps moving finger. once panning gesture ends create map overlay "pin" map , clean drawing view. need careful when redrawing view, should each time specify rect redrawn , redraw in rect, redrawing whole thing each time cause flicker in view well. more coding involved, should work. check question how incrementally draw on view.
Comments
Post a Comment