-1

I want to achieve this following route poly line view over Apple map.I want this type of route polyLine

And want to show poly line on road, which connect from source to destination.

my code for viewcontroller header file is...

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface TrackViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapView;

@property (strong,nonatomic) NSMutableArray *arrAnnotation;
@property (nonatomic, retain) MKPolyline *polyLine;
@property (nonatomic, retain) MKPolylineView *polyLineView;

@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) CLLocation *currentLocation;

my code for viewcontroller implementation file is...

- (void)viewDidLoad {
[super viewDidLoad];

_mapView.showsUserLocation = YES;

if ([CLLocationManager locationServicesEnabled]) {
    if (self.locationManager == nil) {
        self.locationManager = [[CLLocationManager alloc]init];
        self.locationManager.delegate = self;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        self.locationManager.distanceFilter = kCLDistanceFilterNone;
    }
    [self.locationManager startUpdatingLocation];
}

NSArray *name=[[NSArray alloc]initWithObjects:
               @"Mumbai",
               @"Chennai", nil];
self.arrAnnotation=[[NSMutableArray alloc]initWithCapacity:name.count];

MKPointAnnotation *mappin1, *mappin2;

CLLocationCoordinate2D location[3];

mappin1 = [[MKPointAnnotation alloc]init];
location[0] = CLLocationCoordinate2DMake(19.129275,72.905273);
mappin1.coordinate=location[0];
mappin1.title=[name objectAtIndex:0];
[self.arrAnnotation addObject:mappin1];

mappin2 = [[MKPointAnnotation alloc]init];
location[1] = CLLocationCoordinate2DMake(13.063426,80.288086);
mappin2.coordinate=location[1];
mappin2.title=[name objectAtIndex:1];
[self.arrAnnotation addObject:mappin2];

[self.mapView addAnnotations:self.arrAnnotation];
self.mapView.mapType = MKMapTypeStandard;
self.mapView.showsUserLocation = YES;

self.polyLine = [MKPolyline polylineWithCoordinates:location count:2];
[self.mapView setVisibleMapRect:[self.polyLine boundingMapRect]];
[self.mapView addOverlay:self.polyLine];
}

and my out is...this is my output

Please help me... Thanks in advance.

Anupam Das
  • 67
  • 10
  • https://www.raywenderlich.com/166182/mapkit-tutorial-overlay-views – Nitish Mar 29 '18 at 13:58
  • Thank you but want it in Objective c – Anupam Das Mar 29 '18 at 14:03
  • 1
    Possible duplicate of [is there a way to get directions in mkmapview using a built in apple API?](https://stackoverflow.com/questions/19772900/is-there-a-way-to-get-directions-in-mkmapview-using-a-built-in-apple-api) – LorenzOliveto Mar 29 '18 at 14:05
  • Possible duplicate of [Finding Path/Route Between two points on MapKit in iPhone](https://stackoverflow.com/questions/12002179/finding-path-route-between-two-points-on-mapkit-in-iphone) – Ankit Jayaswal Mar 29 '18 at 19:21

1 Answers1

1

Your location array should not be filled with only two coordinates. Because adding only two coordinates will join the two points together in a straight line. So you need to add more coordinates to your location array in order to have a more accurate polyline.

DionizB
  • 1,487
  • 2
  • 10
  • 20