0

The application is getting crashed while calculating Map route. The app is running fine with iPhone 5s( iOS 7.1.1) as well as iPad 2(iOS 8.2) but it crashes on iPhone 4s(iOS 8.2) device. Check the below error.

api url: http://maps.apple.com/maps?    output=dragdir&saddr=19.109446,73.016495&daddr=5.432594,100.312103
 2015-10-15 09:30:17.228 PAMB[1127:218193]     NSURLConnection/CFURLConnection HTTP load failed     (kCFStreamErrorDomainSSL, -9814)
 2015-10-15 09:30:17.234 PAMB[1127:217838] *** Terminating app due to  uncaught exception 'NSInvalidArgumentException', reason: 'Invalid Region  <center:+0.00000000, +0.00000000 span:-179.80000000, -359.80000000>'

It exactly crash on this below line

-(NSArray*) calculateRoutesFrom:(CLLocationCoordinate2D) f to: (CLLocationCoordinate2D) t {
NSString* saddr = [NSString stringWithFormat:@"%f,%f", f.latitude, f.longitude];
NSString* daddr = [NSString stringWithFormat:@"%f,%f", t.latitude, t.longitude];

NSString* apiUrlStr = [NSString  stringWithFormat:@"http://maps.google.com/maps?  output=dragdir&saddr=%@&daddr=%@", saddr, daddr];
NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];
 NSLog(@"api url: %@", apiUrl);
NSError* error = nil;
NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl  encoding:NSASCIIStringEncoding error:&error];
NSString* encodedPoints = [apiResponse stringByMatching:@"points:\\\" ([^\\\"]*)\\\"" capture:1L];
if (error) {
    NSLog(@"%@", [error localizedDescription]);
} else {
    NSLog(@"Data has loaded successfully.");
}

return [self decodePolyLine:[encodedPoints mutableCopy]];
}

-(void) showRouteFrom: (Place*) f to:(Place*) t {

if(routes) {
    [mapView removeAnnotations:[mapView annotations]];
    [routes release];
}

PlaceMark* from = [[[PlaceMark alloc] initWithPlace:f] autorelease];
PlaceMark* to = [[[PlaceMark alloc] initWithPlace:t] autorelease];

[mapView addAnnotation:from];
[mapView addAnnotation:to];

routes = [[self calculateRoutesFrom:from.coordinate to:to.coordinate] retain];

[self updateRouteView];
[self centerMap];
}

-(void) updateRouteView {
CGContextRef context =  CGBitmapContextCreate(nil, 
                                              routeView.frame.size.width, 
                                              routeView.frame.size.height, 
                                              8, 
                                              4 * routeView.frame.size.width,
                                              CGColorSpaceCreateDeviceRGB(),
                                              kCGImageAlphaPremultipliedLast);

CGContextSetStrokeColorWithColor(context, lineColor.CGColor);
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
CGContextSetLineWidth(context, 3.0);

for(int i = 0; i < routes.count; i++) {
    CLLocation* location = [routes objectAtIndex:i];
    CGPoint point = [mapView convertCoordinate:location.coordinate toPointToView:routeView];

    if(i == 0) {
        CGContextMoveToPoint(context, point.x, routeView.frame.size.height - point.y);
    } else {
        CGContextAddLineToPoint(context, point.x, routeView.frame.size.height - point.y);
    }
}

CGContextStrokePath(context);

CGImageRef image = CGBitmapContextCreateImage(context);
UIImage* img = [UIImage imageWithCGImage:image];

routeView.image = img;
CGContextRelease(context);

}

-(void) centerMap {
MKCoordinateRegion region;

CLLocationDegrees maxLat = -90;
CLLocationDegrees maxLon = -180;
CLLocationDegrees minLat = 90;
CLLocationDegrees minLon = 180;
for(int idx = 0; idx < routes.count; idx++)
{
    CLLocation* currentLocation = [routes objectAtIndex:idx];
    if(currentLocation.coordinate.latitude > maxLat)
        maxLat = currentLocation.coordinate.latitude;
    if(currentLocation.coordinate.latitude < minLat)
        minLat = currentLocation.coordinate.latitude;
    if(currentLocation.coordinate.longitude > maxLon)
        maxLon = currentLocation.coordinate.longitude;
    if(currentLocation.coordinate.longitude < minLon)
        minLon = currentLocation.coordinate.longitude;
}
region.center.latitude     = (maxLat + minLat) / 2;
region.center.longitude    = (maxLon + minLon) / 2;
region.span.latitudeDelta  = maxLat - minLat+0.2;
region.span.longitudeDelta = maxLon - minLon+0.2;

//Code is crashing here on the below line...
[mapView setRegion:region animated:YES];
}

What can be the solution to work this Map module on every device. Thanks in advance!!

Navnath Memane
  • 265
  • 1
  • 8
  • 25
  • The error is about your `NSURLConnection`, so you should post code about how you perform the `NSULRConnection`. You can also refer to [this answer](http://stackoverflow.com/a/22674004/4195406) for how to do HTTP request correctly. – ztan May 12 '15 at 16:03
  • 1
    I have updated my code. Check the "calculateRoutesFrom" method in which map.google url is called. Also this error occurs on iPhone 4(iOS 8.2) device only. – Navnath Memane May 13 '15 at 07:04

0 Answers0