I'm making an app that uses the use of coordinate data. My app would launch, and the other will have to go through a view different view controllers before getting to this stage. The user will select a coordinate route before entering this particular UIViewController
.
I currently, for the sake of testing purposes have all this being done in viewDidLoad
. The purpose of this view controller is to display a map with a MKPolyline
based on a route that is predetermined. The MKPolyline
will be a gradient based on a factor, in this case, velocity
.
GradientPolylineOverlay
is from https://github.com/wdanxna/GradientPolyline, and Gradient Polyline with MapKit ios.
My main issue is that, with my code as is, the rendering of the MKPolyline
is very slow, and any events on the mapView
are slow and laggy.
I have determined since I have many coordinates (this can easily be over 7000) that one of the two following lines are causing the issue:
[mapView addOverlay:polyline];
[self.view addSubview:mapView];
- (void)viewDidLoad
{
[super viewDidLoad];
data = delegate.data; // contains sorted/parse data from my source
NSInteger pointsCount = [data sampleCount];
// set up mapView
mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
mapView.delegate = self;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(*[data getGPSCoordinatesForSample:0], 500, 500);
[mapView setRegion:region];
CLLocationCoordinate2D pointsToUse[pointsCount];
float* velocity =0;
velocity = malloc(sizeof(float)*pointsCount);
// pointCount contains 7000 coordinates!
for(int i = 0; i < pointsCount; i++) {
CGPoint p = CGPointMake([data getGPSCoordinatesForSample:i]->latitude,[data getGPSCoordinatesForSample:i]->longitude);
pointsToUse[i] = CLLocationCoordinate2DMake(p.x,p.y);
velocity[i] =[data getGPSVelocityForSample:i];
}
polyline = [[GradientPolylineOverlay alloc] initWithPoints:pointsToUse velocity:velocity count:pointsCount];
// issues!
[mapView addOverlay:polyline];
[self.view addSubview:mapView];
free(velocity);
}
I am lead to believe that it this point, to boost the performance, I should use Grand Central Dispatch. Using GCD, I altered my code to:
dispatch_queue_t queue = dispatch_queue_create("Test", 0);
dispatch_async(queue, ^{
CLLocationCoordinate2D pointsToUse[pointsCount];
float* velocity =0;
velocity = malloc(sizeof(float)*pointsCount);
// pointCount contains 7000 coordinates!
for(int i = 0; i < pointsCount; i++) {
CGPoint p = CGPointMake([data getGPSCoordinatesForSample:i]->latitude,[data getGPSCoordinatesForSample:i]->longitude);
pointsToUse[i] = CLLocationCoordinate2DMake(p.x,p.y);
velocity[i] =[data getGPSVelocityForSample:i];
}
polyline = [[GradientPolylineOverlay alloc] initWithPoints:pointsToUse velocity:velocity count:j];
[mapView addOverlay:polyline]; // Crash
dispatch_async(dispatch_get_main_queue(), ^{
[self.view addSubview:mapView];
});
});
[mapView addOverlay:polyline];
gives off a Thread 4: EXC_BAD_ACCESS
debug error.
My ideal solution would have to use all the coordinates, but I can scale this down if required. If I can use GCD at some place in the code, where would the best place be?
Thank you!