I have to solve a problem. I have stored on the server a lot of coordinates, they represent a course, and I have to draw the course on the map, must support iOS6 and iOS7
So, should be able to draw something like this
Can anyone help me with solutions or ideas to better achieve this?
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
来源:https://stackoverflow.com/questions/21697040/ios-maps-draw-route-line-between-several-points-geopoints