V
V
Vadim Prokhorov2016-07-11 03:49:55
Objective-C
Vadim Prokhorov, 2016-07-11 03:49:55

What is the reason for the program to crash when building a route from point A to B?

I'm using google maps in an ios app that builds a route from my current location to a saved position using the google maps directions API.
Route building code:

-(void) getRoadTo {
    rectangle.map = nil;
//    NSString *str= [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&sensor=true&mode=walking&KEY=AIzaSyDCLfvIDIxLALbEOFufd_lZzn_rJbpAXqg", userLocation.coordinate.latitude, userLocation.coordinate.longitude, [Singleton sharedSingleton].location.coordinate.latitude, [Singleton sharedSingleton].location.coordinate.longitude];
    
    NSString *str= [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&sensor=true&mode=walking&KEY=AIzaSyDCLfvIDIxLALbEOFufd_lZzn_rJbpAXqg", userLocation.coordinate.latitude, userLocation.coordinate.longitude, 55.50998, 37.1337];
    
    NSURL *url=;
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSError *error;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    NSArray *latestRoutes = [json objectForKey:@"routes"];
    NSString *points=[;
    rectangle.map = mapView;
    rectangle.strokeColor = [UIColor colorWithRed:99./256. green:174./256. blue:197./256. alpha:1.];
    rectangle.strokeWidth = 5.0;
    
    //--Вариант 1--//
    
//    @try {
//        NSArray *temp= [self decodePolyLine:[points mutableCopy]];
//        GMSMutablePath *path = [GMSMutablePath path];
//    
//        for(int idx = 0; idx < [temp count]; idx++){
//            CLLocation *location = [temp objectAtIndex:idx];
//            [path addCoordinate:location.coordinate];
//        }
//        
//        
//
//        
//
//        rectangle = [GMSPolyline polylineWithPath:path];
//        rectangle.strokeWidth = 5.0;
//        rectangle.map = mapView;
//        rectangle.strokeColor = [UIColor colorWithRed:99./256. green:174./256. blue:197./256. alpha:1.];
//    }
//    @catch (NSException * e) {
//    }
}

Decoding method for the first option:
-(NSMutableArray *)decodePolyLine: (NSMutableString *)encoded {
    [encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\"
                                options:NSLiteralSearch
                                  range:NSMakeRange(0, [encoded length])];
    NSInteger len = [encoded length];
    NSInteger index = 0;
    NSMutableArray *array = [[NSMutableArray alloc] init] ;
    NSInteger lat=0;
    NSInteger lng=0;
    while (index < len) {
        NSInteger b;
        NSInteger shift = 0;
        NSInteger result = 0;
        do {
            b = [encoded characterAtIndex:index++] - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
        lat += dlat;
        shift = 0;
        result = 0;
        do {
            b = [encoded characterAtIndex:index++] - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
        lng += dlng;
        NSNumber *latitude = [[NSNumber alloc] initWithFloat:lat * 1e-5] ;
        NSNumber *longitude = [[NSNumber alloc] initWithFloat:lng * 1e-5] ;
        CLLocation *loc = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]] ;
        [array addObject:loc];
    }
    
    return array;
}

Everything would be fine if, at a large distance from point A to B, the program would not crash with an error:
994b448899564a03b8bc6b0ad222376f.png
What is the reason I can’t imagine ..

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
A
Alexander, 2016-07-11
@alexyat

it is also written there what is the reason, referring to an empty array to element 0

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question