1

I draw path on MKMapView based on coordinates stored in SQLite on iPhone.
But now I stored 14000 coordinates (just lat/lng) in database and now when I want to display overlay path I get application crash.
My question is is there any way to optimize this code to be faster? This is in view did load:

// ar is NSMutableArray and it is populate from database for a few seconds but code bellow cost me app crash
    for(Path* p in ar)
        {
            self.routeLine = nil;
            self.routeLineView = nil;

            // while we create the route points, we will also be calculating the bounding box of our route
            // so we can easily zoom in on it.
            MKMapPoint northEastPoint;
            MKMapPoint southWestPoint;

            // create a c array of points.
            MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * ar.count);

            for(int idx = 0; idx < ar.count; idx++)
            {
                Path *m_p = [ar objectAtIndex:idx];

                CLLocationDegrees latitude  = m_p.Latitude;
                CLLocationDegrees longitude = m_p.Longitude;

                // create our coordinate and add it to the correct spot in the array
                CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);


                MKMapPoint point = MKMapPointForCoordinate(coordinate);

                // adjust the bounding box
                // if it is the first point, just use them, since we have nothing to compare to yet.
                if (idx == 0) {
                    northEastPoint = point;
                    southWestPoint = point;
                }
                else
                {
                    if (point.x > northEastPoint.x)
                        northEastPoint.x = point.x;
                    if(point.y > northEastPoint.y)
                        northEastPoint.y = point.y;
                    if (point.x < southWestPoint.x)
                        southWestPoint.x = point.x;
                    if (point.y < southWestPoint.y)
                        southWestPoint.y = point.y;
                }

                pointArr[idx] = point;
            }

            // create the polyline based on the array of points.
            self.routeLine = [MKPolyline polylineWithPoints:pointArr count:ar.count];

            _routeRect = MKMapRectMake(southWestPoint.x, southWestPoint.y, northEastPoint.x - southWestPoint.x, northEastPoint.y - southWestPoint.y);
            // clear the memory allocated earlier for the points
            free(pointArr);


            [self.mapView removeOverlays: self.mapView.overlays];
            // add the overlay to the map
            if (nil != self.routeLine) {
                [self.mapView addOverlay:self.routeLine];
            }



UPDATE

ViewDidLoad:

...
[self performSelectorInBackground:@selector(drawPathInBackground) withObject:nil];
...
-(void)drawPathInBackground{
for(int idx = 0; idx < ar.count; idx++)
    { ... }
[self.mapView performSelector:@selector(addOverlay:) onThread:[NSThread mainThread] withObject:self.routeLine waitUntilDone:YES];
}

I did like this and UI not freezes.
The only thing that left is how to draw MKPolyLine on every X points?

6
  • I get application crash yup okay...
    – Rui Peres
    Aug 19, 2013 at 10:03
  • it's not always crash. It simply needs too long time to load 4-5 minutes. That is too much. Is there any way (better way) to draw overlay path faster?
    – 1110
    Aug 19, 2013 at 10:13
  • The outer loop does for (p in ar) and the inner loop does for (idx < ar.count) so essentially they seem to be looping through the same array which results in ar.count * ar.count iterations. Is that correct and necessary?
    – user467105
    Aug 19, 2013 at 11:59
  • You are right. I just realized that outer loop is not necessary so I removed it. But it's still very slow. User probably will think that application stop responding and will kill it. And worse scenario is when I have 100.000 coordinates in database :(
    – 1110
    Aug 19, 2013 at 13:28
  • C'mon any help. Am I put too small bounty or nobody never had issue like this???
    – 1110
    Aug 22, 2013 at 11:15

4 Answers 4

4
+50

three approaches:

  1. don't display every point but rather combine nearby points to just one. the saving depends on your data and the necessity to display all.
  2. if possible load the data in a background thread and display in multiple batches on the main thread. the user will practically see how the data is loaded after time.
  3. load and display data lazily. means: only display those points which are visible on screen
9
  • I tried now to do some background business. I have put all code that draw path in NSOperationQueue handler function and display HUD. But it still takes too long to draw path but at least interface is responsive not blocking now. What do you mean by 'in multiple batches'?
    – 1110
    Aug 21, 2013 at 13:10
  • e.g. 1000 points per batch (or even less). You load 1000 points, display them and then go on to the next 1000 and so on. If you aren't already doing this ;)
    – user207616
    Aug 22, 2013 at 7:59
  • I updated question based on your suggestion you can check. I put code to draw path on every 500 points but it doesn't draw it part by part it still wait and draw at the end whole path. I debug it and breakpoint stop in that IF but doesn't draw path.
    – 1110
    Aug 22, 2013 at 8:42
  • yep, this wouldn't work. Put that code into another function and call it from a background thread. (remember that everything you do in this method is on the background thread, so you should call the drawing on the main thread again)
    – user207616
    Aug 22, 2013 at 9:22
  • I have updated question. Call from background function but again doesn't draw partially.
    – 1110
    Aug 22, 2013 at 10:06
4

Do fetching from the database and processing in a background thread.

Then reduce the number of coordinates in the path using the Douglas–Peucker algorithm:

Douglas–Peucker algorithm

And cache the results.

1

If you have array of coordinates the use this code

here routes is array of coordinates.

NSLog(@"count %d",[routes count]);
MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * [routes count]);

for(int idx = 0; idx < [routes count]; idx++)
{
    CLLocation* location = [routes objectAtIndex:idx];
    CLLocationCoordinate2D workingCoordinate;       
    workingCoordinate.latitude=location.coordinate.latitude;
    workingCoordinate.longitude=location.coordinate.longitude;  
    NSLog(@"loop = %f,%f",workingCoordinate.latitude, workingCoordinate.longitude);
    MKMapPoint point = MKMapPointForCoordinate(workingCoordinate);
    pointArr[idx] = point;      
}   
// create the polyline based on the array of points. 
self.routeLine = [MKPolyline polylineWithPoints:pointArr count:[routes count]];
[mapView addOverlay:self.routeLine];
free(pointArr);

Hope this helps.

0

google has a algorithm that can encode the locations to string. for your situation , 14000 coordinates will be encoded to a String nearly 14000 length. then put the String into sqlite. it will accelerate the speed to get data from DB

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.