Skip to content

Instantly share code, notes, and snippets.

@siqin
Created July 25, 2012 08:17
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save siqin/3175036 to your computer and use it in GitHub Desktop.
Save siqin/3175036 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
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
if(overlay == self.routeLine) {
if(nil == self.routeLineView) {
self.routeLineView = [[[MKPolylineView alloc] initWithPolyline:self.routeLine] autorelease];
self.routeLineView.fillColor = [UIColor redColor];
self.routeLineView.strokeColor = [UIColor redColor];
self.routeLineView.lineWidth = 5;
}
return self.routeLineView;
}
return nil;
}
@EnmanuelVelasco
Copy link

amigo y en swift como se implementa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment