Skip to content

Instantly share code, notes, and snippets.

@donly
Forked from siqin/routeOnMap.m
Last active January 20, 2016 07:45
Show Gist options
  • Save donly/ae074862479c2baaf81f to your computer and use it in GitHub Desktop.
Save donly/ae074862479c2baaf81f to your computer and use it in GitHub Desktop.
Draw route on MKMapView
#pragma mark -
- (void)drawTestLine
{
// test code : draw line between Beijing and Hangzhou
CLLocation *location0 = [[CLLocation alloc] initWithLatitude:39.954245 longitude:116.312455];
CLLocation *location1 = [[CLLocation alloc] initWithLatitude:30.247871 longitude:120.127683];
NSArray *array = [NSArray arrayWithObjects:location0, location1, nil];
[self drawLineWithLocationArray:array];
}
- (void)drawLineWithLocationArray:(NSArray *)locationArray
{
int pointCount = [locationArray count];
CLLocationCoordinate2D *coordinateArray = (CLLocationCoordinate2D *)malloc(pointCount * sizeof(CLLocationCoordinate2D));
for (int i = 0; i < pointCount; ++i) {
CLLocation *location = [locationArray objectAtIndex:i];
coordinateArray[i] = [location coordinate];
}
self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:pointCount];
[self.mapView setVisibleMapRect:[self.routeLine boundingMapRect]];
[self.mapView addOverlay:self.routeLine];
free(coordinateArray);
coordinateArray = NULL;
}
#pragma mark - MKMapViewDelegate
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id <MKOverlay>)overlay {
if (overlay == self.routeLine) {
if (nil == self.lineRender) {
self.lineRender = [[MKPolylineRenderer alloc] initWithPolyline:self.routeLine];
self.lineRender.strokeColor = [UIColor redColor];
self.lineRender.lineWidth = 5.f;
}
return self.lineRender;
}
return nil;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment