iOS Maps draw route (line) between several points (geopoints) [closed]

▼魔方 西西 提交于 2019-12-03 22:01:01

You can do like that :

 - (void)viewDidLoad {
        [super viewDidLoad];

        // center map
        CLLocationCoordinate2D startCoord = CLLocationCoordinate2DMake(47.081012, 2.398781);
        MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:MKCoordinateRegionMakeWithDistance(startCoord, 3000000, 3000000)];
        [self.mapView setRegion:adjustedRegion animated:YES];

        [self showLines];
    }

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
    }

    - (void)showLines {
        CLLocationCoordinate2D *pointsCoordinate = (CLLocationCoordinate2D *)malloc(sizeof(CLLocationCoordinate2D) * 3);
        pointsCoordinate[0] = CLLocationCoordinate2DMake(48.856614, 2.352221);
        pointsCoordinate[1] = CLLocationCoordinate2DMake(45.764043, 4.835658);
        pointsCoordinate[2] = CLLocationCoordinate2DMake(43.296482, 5.369779);


        MKPolyline *polyline = [MKPolyline polylineWithCoordinates:pointsCoordinate count:3];
        free(pointsCoordinate);

        [self.mapView addOverlay:polyline];
    }


    - (MKPolylineRenderer *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay{

        // create a polylineView using polyline overlay object
        MKPolylineRenderer *polylineView = [[MKPolylineRenderer alloc] initWithPolyline:overlay];

// Custom polylineView
        polylineView.strokeColor =  [UIColor orangeColor];   
        polylineView.lineWidth = 2.0;
        polylineView.alpha = 0.5;

        return polylineView;
    }

Having this points you can create a polyline containing all the points you want to connect and then add this polyline as an overlay to the map. By map delegate method you can even customise look of this line to your special needs.

I have created lately a rectangle on the Apple's map which is a simpler example of what you want. You can look at the code and probably it will help you. If you need more help please take a look at this

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!