ios - Does anyone have any examples on how to create a path using MKOverlayPathView? -
i've been looking @ apple's ios class reference documentation, , unfortunately none wiser. have downloaded sample code kmlviewer they've overcomplicated it... want know how generate path , add mkmapview. documentation talks of using cgpathref, doesn't explain how.
could or point me in right direction please? thanks!
here's how generate path , add overlay mkmapview. i'm going use mkpolylineview, subclass of mkoverlaypathview , shields having refer cgpath since instead create mkpolyline (containing data of path) , use create mkpolylineview (the visual representation of data on map).
the mkpolyline has created c array of points (mkmappoint), or c array of coordinates (cllocationcoordinate2d). it's shame mapkit doesn't use more advanced data structures such nsarray, it! i'm going assume have nsarray or nsmutablearray of cllocation objects demonstrate how convert c array of data suitable mkpolyline. array called locations , how fill determined app - e.g. filled in processing touch locations user, or filled data downloaded web service etc.
in view controller in charge of mkmapview:
int numpoints = [locations count]; if (numpoints > 1) { cllocationcoordinate2d* coords = malloc(numpoints * sizeof(cllocationcoordinate2d)); (int = 0; < numpoints; i++) { cllocation* current = [locations objectatindex:i]; coords[i] = current.coordinate; } self.polyline = [mkpolyline polylinewithcoordinates:coords count:numpoints]; free(coords); [mapview addoverlay:self.polyline]; [mapview setneedsdisplay]; } note self.polyline declared in .h as:
@property (nonatomic, retain) mkpolyline* polyline; this view controller should implement mkmapviewdelegate method:
- (mkoverlayview*)mapview:(mkmapview*)themapview viewforoverlay:(id <mkoverlay>)overlay { mkpolylineview* lineview = [[[mkpolylineview alloc] initwithpolyline:self.polyline] autorelease]; lineview.fillcolor = [uicolor whitecolor]; lineview.strokecolor = [uicolor whitecolor]; lineview.linewidth = 4; return lineview; } you can play fillcolor, strokecolor , linewidth properties ensure appropriate app. i've gone simple, moderately wide plain white line here.
if want remove path map, e.g. update new coordinates, do:
[mapview removeoverlay:self.polyline]; self.polyline = nil; and repeat above process create new mkpolyline , add map.
although on first glance mapkit can bit scary , complex, can easy things illustrated in example. scary bit - non-c programmers @ least - use of malloc create buffer, copy cllocationcoordinates using array syntax, , freeing memory buffer afterwards.
Comments
Post a Comment