Two coloured/custom line on MKPolylineRenderer - ios

I am drawing x amount of lines on an MKMapView. The data is being downloaded from a webservice and for each line that needs drawing I am creating a MKPolyline, adding it to an overlayArray (some overlays have more than one polyline) and finally adding that overlay to the map via:
[self.mapView addOverlays:overlayArray];`
The next function is therefore called which works wonderfully:
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
if (![overlay isKindOfClass:[MKPolygon class]]) {
MKPolyline *route = overlay;
MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:route];
renderer.strokeColor = [lineColours objectForKey:route.title];
renderer.lineWidth = 3.0;
return renderer;
} else {
return nil;
}
}
As you can see, the strokeColor gets the correct UIColor from a pre-defined NSMutableDictionary. This all works as expected.
However, some of the lines overlap and using a single colour is not always desired. I was wondering if there was a way of creating a line made up of two or even 3 colours, but not a gradient along the line as seen in many fitness apps, but colours across the line. For example, another use would be to draw a motorway route made up of two white lines with a transparent strip in the middle.
This is quite important for an app I am developing so I will post any findings I find here, and I hope that other people can share their thoughts and knowledge too.

Related

MKPolyline / MKPolylineRenderer changing color without remove it

I working around with map app, I want to ask how to change the polyline color without remove and add it again, I found this topic https://stackoverflow.com/questions/24226290/mkpolylinerenderer-change-color-without-removing-overlay in stackoverflow but this is not involve with my question, I did not touch the line, so no need to do with -[MKMapViewDelegate mapView:didSelectAnnotationView:]
So is it possible to do that?
EDIT: What I want to do is change the polyline color smoothly (by shading a color - sound like an animation) If you have any idea on how to animate this polyline please also tell me too. Thanks
Complex animations or shading/gradients will probably require creating a custom overlay renderer class.
These other answers give ideas about how to draw gradient polylines and animations will most like require a custom overlay renderer as well:
how to customize MKPolyLineView to draw different style lines
Gradient Polyline with MapKit ios
Draw CAGradient within MKPolyLineView
Apple's Breadcrumb sample app also has an example of a custom renderer which you may find useful.
However, if you just want to update the line's color (say from blue to red), then you may be able to do that as follows:
Get a reference to the MKPolyline you want to change.
Get a reference to the MKPolylineRenderer for the polyline obtained in step 1. This can be done by calling the map view's rendererForOverlay: instance method (not the same as the mapView:rendererForOverlay: delegate method.
Update the renderer's strokeColor.
Call invalidatePath on the renderer.
Not sure what you want but you may be able to "animate" the color going from blue to red by changing the color and calling invalidatePath gradually in timed steps.
Another important thing is to make sure the rendererForOverlay delegate method also uses the line's "current" color in case the map view calls the delegate method after you've changed the renderer's strokeColor directly.
Otherwise, after panning or zooming the map, the polyline's color will change back to whatever's set in the delegate method.
You could keep the line's current color in a class-level variable and use that in both the delegate method and the place where you want to change the line's color.
An alternative to a class-level variable (and probably better) is to either use the MKPolyline's title property to hold its color or a custom polyline overlay class (not renderer) with a color property.
Example:
#property (nonatomic, strong) UIColor *lineColor;
//If you need to keep track of multiple overlays,
//try using a NSMutableDictionary where the keys are the
//overlay titles and the value is the UIColor.
-(void)methodWhereYouOriginallyCreateAndAddTheOverlay
{
self.lineColor = [UIColor blueColor]; //line starts as blue
MKPolyline *pl = [MKPolyline polylineWithCoordinates:coordinates count:count];
pl.title = #"test";
[mapView addOverlay:pl];
}
-(void)methodWhereYouWantToChangeLineColor
{
self.lineColor = theNewColor;
//Get reference to MKPolyline (example assumes you have ONE overlay)...
MKPolyline *pl = [mapView.overlays objectAtIndex:0];
//Get reference to polyline's renderer...
MKPolylineRenderer *pr = (MKPolylineRenderer *)[mapView rendererForOverlay:pl];
pr.strokeColor = self.lineColor;
[pr invalidatePath];
}
-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
if ([overlay isKindOfClass:[MKPolyline class]]) {
MKPolylineRenderer *pr = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
pr.strokeColor = self.lineColor;
pr.lineWidth = 5;
return pr;
}
return nil;
}
Yous hould look at MKOverlayPathRenderer method
- invalidatePath.
From the doc, it says:
Call this method when a change in the path information would require
you to recreate the overlay’s path. This method sets the path property
to nil and tells the overlay renderer to redisplay its contents.
So, at this moment, you should be able to change your drawing color.

How to draw path with MapKit in real time (iOS7+)

I'm trying to figure out how to draw a path with MapKit given an array of location (lat/long) points. I think I need to use MKPolyline and MKOverlayRenderer. I can only seem to find information on MKRoute and MKDirections, but this is not what I need.
To start with I will have one point. Every 10 seconds or so another point will be added. How can I draw a line on a map in real time given an array of points, where the array grows over time?
This is all I have so far:
- (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;
}
return nil;
}
You should be able to do this with MKPolyline and MKPolylineRenderer as you have, but also look into -[MKOverlayRenderer setNeedsDisplayInMapRect:] and the associated conversion routines to translate MKMapRect to CGRect (if necessary) and you should be able to force a screen refresh upon receipt of new data.

MKMapView overlay Polyline is drawn on top of road label

I am making the navigation application by drawing a direction between 2 points. I successfully archive the functionality.
But the direction line is drawn on top of the road label and that label cannot be read as show in the picture below.
this is my code to draw overlay
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
MKPolylineView *overlayView = [[MKPolylineView alloc] initWithPolyline:overlay];
overlayView.lineWidth = 10.0f;
//overlayView.strokeColor = [[UIColor redColor] colorWithAlphaComponent:0.5f];
overlayView.strokeColor = [UIColor redColor];
return overlayView;
}
I can overcome this with a transparent line but it is not the efficient way.
The best way is to draw the line between the map layer and label layer of MKMapView but i don't know how can i archive that.
So any help please.
Thanks.
Assuming you are writing this for iOS maps (not google maps as you tagged the question) and using iOS 7 then when you add the overlay to the map view you have the option of defining which level it is on addOverlay:level:. The levels are defined in the MKMapView class reference
[theMapView addOverlay:theOverlay level:MKOverlayLevelAboveRoads];

Multiple Polygon overlays from array

I have an array with polygons created from a data file with coordinates per polygon.
So when I plot them on my map I use:
[mapView addOverlays:polygonArray];
and in my viewForOverlay:
if ([overlay isKindOfClass:[MKPolygon class]]) {
MKPolygonView *polyView = [[MKPolygonView alloc] initWithPolygon:overlay];
polyView.fillColor = [[UIColor redColor] colorWithAlphaComponent:0.1];
polyView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.1];
polyView.lineWidth = 1;
return polyView;
}
else {
return nil;
}
The problem is that regarding my "colorWithAlphaComponent" the code seems to reuse and recreate the polyView for each Polygon. Therefore the first one is with alpha 0.1 but the second is 2x and so on.. So the last few Polygons aren't "seethrough" anymore.
Here's how it looks:
Based on the problem description and picture, it sounds like you are adding the same polygon multiple times so it gets overlapped with itself.
When overlays overlap, the map view blends their colors together resulting in a darker appearance.
If polygonArray contains unique polygons itself but addOverlays is called multiple times, you should call removeOverlays before addOverlays if the existing polygons on the map are already included in polygonArray.
Another possibility is that polygonArray itself contains duplicate polygons.
Even if addOverlays is called only once, the map will add multiple instances of the same polygon resulting in those overlays overlapping themselves giving them a darker color than expected.
To fix this, you should eliminate the duplication in polygonArray.

MKPolyLine, detect when lines overlap and change color accordingly

I am designing a transit app that overlays several routes in the form of MKPolyLines on a map. Currently the colors of the various routes are set as the title property of MKPolyLine. I'm wondering if there is a way to detect when lines of different colors overlap, and then change the color. Currently when two routes are added on top of each other, the color is simply the last one added.
My attempt at the pseudocode
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {
MKPolyline *polyline = (MKPolyline *)overlay;
UIColor *color = [self colorWithHexString:polyline.title];
MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay];
if the polyline matches an already existing polyline{
color = new color
}
polylineView.strokeColor = color;
polylineView.lineWidth = 5.0;
}
Seems simple enough? Not sure if it is possible to compare polylines and see if one is already on the map, might not be an exact enough identifier. Thanks for your help.
I ended up using the lineDashPattern property of MKPolylineView. Applying this to one of the overlapping lines achieves the desired effect.

Resources