3

I am trying to show route between two locations with the help of this tutorial. They have used a CSV file of coordinates and i am using google api to get coordinates. But Result is completely different. Output

as you can see it is not drawing correct path. Plz suggest me something.

3
  • but i had developed an app using same Direction api and it is still working properly. Its deployment target was 3.2 so now i am developing it again for iOS4 using MKPolyline. Dec 8, 2011 at 6:08
  • the problem is not with the ios but the way your csv file is made and the way you are reading it. Another hint nslog all the coordinates that are forming for making MKPolyline. I bet you will be surprised by what you will see.
    – Robin
    Dec 8, 2011 at 6:28
  • are you using the JSON response..? Dec 8, 2011 at 6:31

2 Answers 2

14

You need to the decode the polyline that you are getting from the response.. and in order to that you need google's algorithm...

// http://code.google.com/apis/maps/documentation/utilities/polylinealgorithm.html  
//  
-(NSMutableArray *)decodePolyLine:(NSString *)encodedStr {  
    NSMutableString *encoded = [[NSMutableString alloc] initWithCapacity:[encodedStr length]];  
    [encoded appendString:encodedStr];  
    [encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\"  
                                options:NSLiteralSearch  
                                  range:NSMakeRange(0, [encoded length])];  
    NSInteger len = [encoded length];  
    NSInteger index = 0;  
    NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease];  
    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] autorelease];  
        NSNumber *longitude = [[[NSNumber alloc] initWithFloat:lng * 1e-5] autorelease];  
        //          printf("[%f,", [latitude doubleValue]);  
        //          printf("%f]", [longitude doubleValue]);  
        CLLocation *loc = [[[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]] autorelease];  
        [array addObject:loc];  
    }  
    [encoded release];  
    return array;  
}

this will give you the mutable array with all the points (in the form of CLLocation objects) and also dont just decode the main polyline .. decode each and every sub polyline that you receive and than plot otherwise the directions will not be proper.

9
  • 4
    i am using same encoding function but app is crashing. On removing [encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\" options:NSLiteralSearch range:NSMakeRange(0, [encoded length])]; it is working but it not giving accurate result. Dec 8, 2011 at 7:51
  • 1
    I also encountered some problem in this... It used to work from small distances ...LIke between AGRA and Delhi, when i used to use delhi and trivandrum it ended up crashing. So I am not sure how it really works.. never gave much thought to it. In the end I ended up loading the webview with google maps. :P Dec 8, 2011 at 8:21
  • 1
    can you suggest something on accuracy. it is not plotting on road Dec 8, 2011 at 8:39
  • 1
    I did mention.. "decode each and every sub polyline that you receive and than plot otherwise the directions will not be proper." Dec 8, 2011 at 8:40
  • 1
    thats what i am doing now but still route is not accurate Dec 8, 2011 at 8:43
0

You can add solid line(Path) by adding this simple method in your code:

-(void)addSolidLine:(CLLocation *)source andDestination:(CLLocation*)destination
{
    CLLocationCoordinate2D coordinates[2] = {source.coordinate, destination.coordinate};
    MKGeodesicPolyline *geodesicPolylineSolid = [MKGeodesicPolyline polylineWithCoordinates:coordinates count:2];
    [self.mapView addOverlay:geodesicPolylineSolid];
}

#pragma mark - map view delegate

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id <MKOverlay>)overlay
{   
    if ([overlay isKindOfClass:[MKPolyline class]]) {  
        MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:(MKPolyline *)overlay];
        renderer.lineWidth = 1.5f;
        renderer.strokeColor = [UIColor greenColor];
        renderer.alpha = 50;
        return renderer;
    }
}

And if you want to show dotted line, you can just add this medthod:

MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:(MKPolyline *)overlay];
renderer.lineDashPattern = @[@2, @5];
renderer.lineWidth = 1.5f;
renderer.strokeColor = [UIColor redColor];
renderer.alpha = 50;
isShowingDottedLine = false;
return renderer;

Your Answer

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.