Question

I'm relatively new to the iOs scenario, so this may be a dumb question but anyways...

I've been working on a project, and I wanted to do a specific route highlighted on the map. For exemple: A bus's itinerary is always the same, so I'd like to highlight the streets that the bus goes through before reaching his final destination.

The thing is I was only able to get directions from one point to another, but it does not follow the bus's itinerary, so it does not work for me.

Please help!!

Était-ce utile?

La solution

First if you require specific route you will need some of its coordinates. Let's say you have some route coordinates as NSDictionary objects like in the example below

{
 Lat = 53.3478;
 Lon = -6.2597;
}

Stored in NSArray called routeLocations then you can do the following:

CLLocationCoordinate2D routeCoord[routeLocations.count];
for (int i = 0; i < routeLocations.count; i++ )
{
 id location =[routeLocations objectAtIndex:i];
 routeCoord[i] = CLLocationCoordine2DMake([[location objectForKey:@"Lat"]floatValue], [[location objectForKey:@"Lon"]floatValue]);
}
// create your route Polyline
MKPolyline *poly = [MKPolyline polylineWithCoordinates:routeCoord count:routeLocations.count];

// first remove previously added overlays if any then add your newly created route polyline
[self.mapView addOverlay:poly];

If you require customization on your overlay you can implement the following For iOS >= 7

-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
  if ([overlay isKindOfClass:[MKPolyline class]])
  {
    MKPolyline *route = overlay;
    MKPolylineRenderer *routeRenderer = [[MKPolylineRenderer alloc]initWithPolyline:route];
    routeRenderer.strokeColor = [UIColor blueColor];
    return routeRenderer;
  }
}

Else use deprecated method

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
  if ([overlay isKindOfClass:[MKPolyline class]])
  {
    MKPolylineView *route = [[MKPolylineView alloc]initWithPolyline:overlay];
    route.strokeColor = [UIColor blueColor];
    return route;
  }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top