Search code examples
objective-cgoogle-mapsios7memory-leaksbearing

Calculating bearing/heading between two coordinates causes memory issue in iOS


We are mimicking google maps navigation in iOS with Google Maps SDK for iOS. In this process I need to calculate bearing/heading between two coordinates for number of times assume 100 times. I am calculating bearing using below method

- (float)getHeadingForDirectionFromCoordinate:(CLLocationCoordinate2D)fromLoc toCoordinate:(CLLocationCoordinate2D)toLoc
{
float fLat = degreesToRadians(fromLoc.latitude);
float fLng = degreesToRadians(fromLoc.longitude);
float tLat = degreesToRadians(toLoc.latitude);
float tLng = degreesToRadians(toLoc.longitude);

float degree = radiandsToDegrees(atan2(sin(tLng-fLng)*cos(tLat), cos(fLat)*sin(tLat)-sin(fLat)*cos(tLat)*cos(tLng-fLng)));

if (degree >= 0) {
    return degree;
} else {
    return 360+degree;
}
}

By this method I am getting exact bearing that I am looking for, But if I call this method for 100 times the memory usage of app is reaching to 2GB dynamically. So my app is crashing with malloc error.

Is there any other way to calculate bearing with low memory management or how can I reduce the memory usage of application by using above calculation only.


Solution

  • The only solution for reducing memory issue is reducing the number of calculations. Calculate the bearing for required points only and remove calculations for unnecessary points.