0

I have been working on MapView on how to show routes etc etc. I have used long press gesture to drop a pin and a polyline is shown between the two pins dropped via long press. Now the polyline which connects the two pins is a straight line, i want to render is properly according to the route on the map. Plz help me. Heres the code

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;
}
iDeveloper
  • 940
  • 1
  • 10
  • 45
  • Possible duplicate of [Drawing a route in MapKit in iPhone SDK](http://stackoverflow.com/questions/2834523/drawing-a-route-in-mapkit-in-iphone-sdk) – Muneeba Jan 05 '16 at 10:29

1 Answers1

0

i am using this one check it might help you

- (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];

}
Muneeba
  • 1,756
  • 10
  • 11
Mahesh reddy
  • 134
  • 13