2016-11-24 80 views
2

我实施了一张地图,效果很好。但是,当我尝试在地图放大,缩小,我看这个问题:地图路径视图无法正确显示

enter image description here

这里是我的代码:

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation 
{ 
    MKAnnotationView* annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"LocationPin"]; 
    if ([annotation isEqual:self.startLocation]) { 
     annotationView.image = [UIImage imageNamed:@"startPin.png"]; 
     return annotationView; 
    } else if ([annotation isEqual:self.endLocation]){ 
     annotationView.image = [UIImage imageNamed:@"endPin.png"]; 
     return annotationView; 
    } 

    return nil; 
} 

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay { 
    @autoreleasepool { 
     if ([overlay isKindOfClass:[MKPolyline class]]) { 
      MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay]; 
      [renderer setStrokeColor:kColor_Magenta]; 
      [renderer setLineWidth:4.0]; 
      return renderer; 
     } 
     return nil; 
    } 

} 
+0

嘿Khuong,你有什么解决办法吗?我面临同样的问题。 –

+0

嗨@kishangodhani,仍然不是,那奇怪的形象仍然出现:( – Khuong

+0

我尝试很多os解决方案,但没有得到任何适当的解决方案 –

回答

0

这可能与缩放时重绘折线做。检查这个答案。 Zoom MKMapView to fit polyline points

+0

嗨@Nick,这个函数'zoomToPolyLine',在'MKMapViewDelegate'中,我没有看到它 – Khuong

+0

它在里面createMKPolylineAnnotation方法 –

+0

我试试这个http://stackoverflow.com/questions/13569327/zoom-mkmapview-to-fit-polyline-points解决方案,但没有为我工作 –

0
-(void)LoadMapRoute:(NSString*)SourceAddress andDestinationAddress:(NSString*)DestinationAdds 
{ 
NSString *strUrl; 
strUrl= [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@&sensor=true",SourceAddress,DestinationAdds]; 
NSLog(@"strUrl=====%@",strUrl); 
strUrl=[strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
NSData *data =[NSData dataWithContentsOfURL:[NSURL URLWithString:strUrl]]; 
NSError* error; 
if (data) { 
    NSDictionary* json = [NSJSONSerialization 
          JSONObjectWithData:data //1 
          options:kNilOptions 
          error:&error]; 
    NSArray *arrRouts=[json objectForKey:@"routes"]; 
    if ([arrRouts isKindOfClass:[NSArray class]]&&arrRouts.count==0) { 
     [self ShowAlert:@"didn't find direction"]; 
     return; 
    } 
    NSArray *arrDistance =[[[json valueForKeyPath:@"routes.legs.steps.distance.text"] objectAtIndex:0]objectAtIndex:0]; 
    NSString *totalDuration = [[[json valueForKeyPath:@"routes.legs.duration.text"] objectAtIndex:0]objectAtIndex:0]; 
    NSString *totalDistance = [[[json valueForKeyPath:@"routes.legs.distance.text"] objectAtIndex:0]objectAtIndex:0]; 
    NSArray *arrDescription =[[[json valueForKeyPath:@"routes.legs.steps.html_instructions"] objectAtIndex:0] objectAtIndex:0]; 
    dictRouteInfo=[NSDictionary dictionaryWithObjectsAndKeys:totalDistance,@"totalDistance",totalDuration,@"totalDuration",arrDistance ,@"distance",arrDescription,@"description", nil]; 
    [self.btnDirection setEnabled:NO]; 
    if (dictRouteInfo) { 
     [self.btnDirection setEnabled:YES]; 
    } 
    NSArray* arrpolyline = [[[json valueForKeyPath:@"routes.legs.steps.polyline.points"] objectAtIndex:0] objectAtIndex:0]; //2 
    double srcLat=[[[[json valueForKeyPath:@"routes.legs.start_location.lat"] objectAtIndex:0] objectAtIndex:0] doubleValue]; 
    double srcLong=[[[[json valueForKeyPath:@"routes.legs.start_location.lng"] objectAtIndex:0] objectAtIndex:0] doubleValue]; 
    double destLat=[[[[json valueForKeyPath:@"routes.legs.end_location.lat"] objectAtIndex:0] objectAtIndex:0] doubleValue]; 
    double destLong=[[[[json valueForKeyPath:@"routes.legs.end_location.lng"] objectAtIndex:0] objectAtIndex:0] doubleValue]; 
    CLLocationCoordinate2D sourceCordinate = CLLocationCoordinate2DMake(srcLat, srcLong); 
    CLLocationCoordinate2D destCordinate = CLLocationCoordinate2DMake(destLat, destLong); 

    [self addAnnotationSrcAndDestination:sourceCordinate :destCordinate andAdds:SourceAddress andDestinationAddress:DestinationAdds]; 
    // NSArray *steps=[[aary objectAtIndex:0]valueForKey:@"steps"]; 

    // replace lines with this may work 

    NSMutableArray *polyLinesArray =[[NSMutableArray alloc]initWithCapacity:0]; 

    for (int i = 0; i < [arrpolyline count]; i++) 
    { 
     NSString* encodedPoints = [arrpolyline objectAtIndex:i] ; 
     MKPolyline *route = [self polylineWithEncodedString:encodedPoints]; 
     [polyLinesArray addObject:route]; 
    } 
    [self.mapShow addOverlays:polyLinesArray]; 
    self.mapShow.delegate = self; 
    [self zoomToFitMapAnnotations:self.mapShow]; 
}else{ 
    [self.btnDirection setEnabled:NO]; 
    [self ShowAlert:@"didn't find direction"]; 
} 
} 
#pragma mark - add annotation on source and destination 

-(void)addAnnotationSrcAndDestination :(CLLocationCoordinate2D)srcCord :(CLLocationCoordinate2D)destCord andAdds:(NSString*)SourceAddress andDestinationAddress:(NSString*)DestinationAdds 
{ 
MKPointAnnotation *sourceAnnotation = [[MKPointAnnotation alloc]init]; 
MKPointAnnotation *destAnnotation = [[MKPointAnnotation alloc]init]; 
sourceAnnotation.coordinate=srcCord; 
destAnnotation.coordinate=destCord; 
sourceAnnotation.title=SourceAddress; 
destAnnotation.title=DestinationAdds; 
[self.mapShow addAnnotation:sourceAnnotation]; 
[self.mapShow addAnnotation:destAnnotation]; 
} 

#pragma mark - decode map polyline 

- (MKPolyline *)polylineWithEncodedString:(NSString *)encodedString { 
const char *bytes = [encodedString UTF8String]; 
NSUInteger length = [encodedString lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; 
NSUInteger idx = 0; 

NSUInteger count = length/4; 
CLLocationCoordinate2D *coords = calloc(count, sizeof(CLLocationCoordinate2D)); 
NSUInteger coordIdx = 0; 

float latitude = 0; 
float longitude = 0; 
while (idx < length) { 
    char byte = 0; 
    int res = 0; 
    char shift = 0; 

    do { 
     byte = bytes[idx++] - 63; 
     res |= (byte & 0x1F) << shift; 
     shift += 5; 
    } while (byte >= 0x20); 

    float deltaLat = ((res & 1) ? ~(res >> 1) : (res >> 1)); 
    latitude += deltaLat; 

    shift = 0; 
    res = 0; 

    do { 
     byte = bytes[idx++] - 0x3F; 
     res |= (byte & 0x1F) << shift; 
     shift += 5; 
    } while (byte >= 0x20); 

    float deltaLon = ((res & 1) ? ~(res >> 1) : (res >> 1)); 
    longitude += deltaLon; 

    float finalLat = latitude * 1E-5; 
    float finalLon = longitude * 1E-5; 

    CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(finalLat, finalLon); 
    coords[coordIdx++] = coord; 

    if (coordIdx == count) { 
     NSUInteger newCount = count + 10; 
     coords = realloc(coords, newCount * sizeof(CLLocationCoordinate2D)); 
     count = newCount; 
    } 
} 
MKPolyline *polyline = [MKPolyline polylineWithCoordinates:coords count:coordIdx]; 
free(coords); 

return polyline; 
} 
#pragma mark - map overlay 
-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay 
{ 
MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay]; 
renderer.strokeColor = [UIColor colorWithRed:155.0/255.0 green:89.0/255.0 blue:182.0/255.0 alpha:1.0]; 
renderer.lineWidth = 5.0; 
return renderer; 
} 
+0

我已经实现了相同你的代码,但我使用MKDirection –