-->

Drawing Path between two locations using MKPolylin

2020-07-18 11:53发布

问题:

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.

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

回答1:

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.



回答2:

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;


标签: iphone ios