如何通过长按手势在MKMapView上渲染两个选定接点之间的路线



我一直在MapView上研究如何显示路线等。我用长按手势放下一个引脚,通过长按放下的两个引脚之间显示了一条折线。现在连接两个接点的多段线是一条直线,我想根据地图上的路线正确渲染。请帮帮我。这是代码

MapView.h

 -(void)viewDidLoad {
  [super viewDidLoad];
  UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(addPin:)];
  recognizer.minimumPressDuration = 0.5;
  [self.mapView2 addGestureRecognizer:recognizer];
 }
-(void)addPin:(UIGestureRecognizer *)recognizer {
if (recognizer.state != UIGestureRecognizerStateBegan) {
    return;
}
// convert touched position to map coordinate
  CGPoint userTouch = [recognizer locationInView:self.mapView2];
  CLLocationCoordinate2D mapPoint = [self.mapView2 convertPoint:userTouch toCoordinateFromView:self.mapView2];
  NSLog(@"Touched Coord :- %f", mapPoint);
  Pin *newPin = [[Pin alloc]initWithCoordinate:mapPoint]; //PIN is NSOBJECT
 newPin.title = @"source";
 [self.mapView2 addAnnotation:newPin];
 [self.allPins addObject:newPin];
 [self drawLines:self];
 }
- (IBAction)drawLines:(id)sender {
[self drawLineSubroutine];
[self drawLineSubroutine];
  }
-(IBAction)undoLastPin:(id)sender {
// grab the last Pin and remove it from our map view
Pin *latestPin = [self.allPins lastObject];
[self.mapView2 removeAnnotation:latestPin];
[self.allPins removeLastObject];
// redraw the polyline
[self drawLines:self];
  }
-(void)drawLineSubroutine {
// remove polyline if one exists
[self.mapView2 removeOverlay:self.polyline];
// create an array of coordinates from allPins
CLLocationCoordinate2D coordinates[self.allPins.count];
int i = 0;
for (Pin *currentPin in self.allPins) {
    coordinates[i] = currentPin.coordinate;
    i++;
}
// create a polyline with all cooridnates
MKPolyline *polyline = [MKPolyline polylineWithCoordinates:coordinates count:self.allPins.count];
[self.mapView2 addOverlay:polyline];
self.polyline = polyline;
// create an MKPolylineView and add it to the map view
self.lineView = [[MKPolylineView alloc]initWithPolyline:self.polyline];
self.lineView.strokeColor = [[UIColor blueColor]colorWithAlphaComponent:0.5];
self.lineView.lineWidth = 7;
// for a laugh: how many polylines are we drawing here?
self.title = [[NSString alloc]initWithFormat:@"%lu", (unsigned long)self.mapView2.overlays.count];
}
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay {
return self.lineView;
}

我正在使用这项检查,它可能会帮助您

- (void)drawRoute:(NSString *)startLat :(NSString *)startLong :(NSString *)DestLat :(NSString *)DestLong :(NSString *)DestName{ pointArr = malloc(sizeof(CLLocationCoordinate2D) * [TotalRoutes count]);
    for(int i = 0; i < [TotalRoutes count]; i++)
    {
    NSDictionary *route=[TotalRoutes objectAtIndex:i];
    pointArr[i]= CLLocationCoordinate2DMake([route[@"Lat"] doubleValue], [route[@"Lng"] doubleValue]) ;
    }
    myPolyline = [MKPolyline polylineWithCoordinates:pointArr count:TotalRoutes.count];
    [_RouteMap addOverlay:myPolyline];
   //  zooming only First time to polyline
    [self zoomToPolyLine:_RouteMap polyline:myPolyline animated:YES];
    [self mapView:_RouteMap viewForAnnotation:annotation2];
}
-(void)zoomToPolyLine: (MKMapView*)map polyline: (MKPolyline*)polyline animated: (BOOL)animated
{  
   [map setVisibleMapRect:[polyline boundingMapRect] edgePadding:UIEdgeInsetsMake(50.0, 50.0, 50.0, 50.0) animated:animated];
}

最新更新