How can I add shadow or edge to MKPolylineRenderer? - ios

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)overlay{
if ([overlay isKindOfClass:[MulticolorPolylineSegment class]]) {
MulticolorPolylineSegment *polyLine = (MulticolorPolylineSegment *)overlay;
MKPolylineRenderer *aRenderer = [[MKPolylineRenderer alloc] initWithPolyline:polyLine];
aRenderer.strokeColor = polyLine.color;
// aRenderer.fillColor = [UIColor redColor];
aRenderer.lineCap = kCGLineCapRound;
aRenderer.lineJoin = kCGLineJoinBevel;
// aRenderer.lineDashPhase = 66;
aRenderer.miterLimit = 40;
aRenderer.lineWidth = 6;
return aRenderer;
}}
The code is showing path for running on the map.I tried many methods and I don't know how to solve it.

You can add a second MKPolyline underneath yours. You then render this one as your shadow.
self.backgroundPolyline = [MKPolyline polylineWithCoordinates:coordinates
count:count];
self.backgroundPolyline.title = BACKGROUND;
[self.mapView insertOverlay:self.backgroundPolyline
belowOverlay:self.routePolyline];
…
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id <MKOverlay>)overlay
{
if ([overlay isKindOfClass:[MKTileOverlay class]]) {
return [[MKTileOverlayRenderer alloc] initWithOverlay:overlay];
}else if ([overlay isKindOfClass:[MKPolyline class]]) {
MKPolylineRenderer* aView = [[MKPolylineRenderer alloc] initWithPolyline:(MKPolyline*)overlay];
if([overlay.title isEqualToString:BACKGROUND]) {
aView.strokeColor = [[UIColor whiteColor] colorWithAlphaComponent:0.5];
aView.lineWidth = 8;
}else{
aView.strokeColor = [[UIColor redColor] colorWithAlphaComponent:1];
aView.lineWidth = 2;
}
return aView;
}
return nil;
}

Related

Prevent MKPolygon to have knots

I'm developing an app with a map in which the user can draw a polygon area.
My issue is what it's possible drawing polygons with knots (see the image) (I don't know if knot is the right word). I didn't find a simply way preventing the polygon to get knots.
For the case of the attached image, I would like the small curl to be removed and even the outline to be smoothed
Do you know a way to make that?
The process of drawing the polygon while the user is touching the screen, does use MKPolyline, MKPolygon and MKOverlay as follows:
- (void)touchesBegan:(UITouch*)touch
{
CGPoint location = [touch locationInView:self.mapView];
CLLocationCoordinate2D coordinate = [self.mapView convertPoint:location toCoordinateFromView:self.mapView];
[self.coordinates addObject:[NSValue valueWithMKCoordinate:coordinate]];
}
- (void)touchesMoved:(UITouch*)touch
{
CGPoint location = [touch locationInView:self.mapView];
CLLocationCoordinate2D coordinate = [self.mapView convertPoint:location toCoordinateFromView:self.mapView];
[self.coordinates addObject:[NSValue valueWithMKCoordinate:coordinate]];
}
- (void)touchesEnded:(UITouch*)touch
{
CGPoint location = [touch locationInView:self.mapView];
CLLocationCoordinate2D coordinate = [self.mapView convertPoint:location toCoordinateFromView:self.mapView];
[self.coordinates addObject:[NSValue valueWithMKCoordinate:coordinate]];
[self didTouchUpInsideDrawButton:nil];
}
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
MKOverlayPathView *overlayPathView;
if ([overlay isKindOfClass:[MKPolygon class]])
{
// create a polygonView using polygon_overlay object
overlayPathView = [[MKPolygonView alloc] initWithPolygon:overlay];
overlayPathView.fillColor = [UIColor redColor];
overlayPathView.lineWidth = 1.5;
return overlayPathView;
}
else if ([overlay isKindOfClass:[MKPolyline class]])
{
overlayPathView = [[MKPolylineView alloc] initWithPolyline:(MKPolyline *)overlay];
overlayPathView.fillColor = [UIColor redColor];
overlayPathView.lineWidth = 3;
return overlayPathView;
}
return nil;
}
MKOverlayPathView was deprecated since iOS 7.0. You'd use MKOverlayRenderer instead of it and also related map delegate method.
Try to play with miterLimit property of MKOverlayRenderer.
Example:
-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
if ([overlay isKindOfClass:[MKPolygon class]]) {
MKPolygonRenderer *polygonRenederer = [[MKPolygonRenderer alloc] initWithPolygon:overlay];
polygonRenederer.fillColor = [UIColor redColor];
polygonRenederer.lineWidth = 1.5;
polygonRenederer.miterLimit = 10;
return polygonRenederer;
} else if ([overlay isKindOfClass:[MKPolyline class]]) {
MKPolylineRenderer *lineRenderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
lineRenderer.strokeColor = [UIColor redColor];
lineRenderer.lineWidth = 3;
return lineRenderer;
}
return nil;
}

'initWithPolygon:' & 'initWithPolyline:' are deprecated: first deprecated in iOS 7.0: is there any solution for this warning in iOS?

I am using KMLParser library for offline map to download .kml file from server but I found these warnings.
Please give any solution to remove these warnings.
Here is function for both:
for initWithPolygon,
- (MKOverlayPathView *)createOverlayView:(MKShape *)shape
{
// KMLPolygon corresponds to MKPolygonView
MKPolygonView *polyView = [[MKPolygonView alloc] initWithPolygon:(MKPolygon *)shape];
return polyView ;
}
for initWithPolyline,
- (MKOverlayPathView *)createOverlayView:(MKShape *)shape
{
// KMLLineString corresponds to MKPolylineView
MKPolylineView *lineView = [[MKPolylineView alloc] initWithPolyline:(MKPolyline *)shape];
return lineView ;
}
You should use (MKOverlayRenderer *) type delegate instead of (MKOverlayView *) type delegate. And return MKPolylineRenderer instead of MKPolylineView.
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView
rendererForOverlay:(id<MKOverlay>)overlay {
MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
renderer.strokeColor = [UIColor blueColor];
renderer.lineWidth = 2.0;
return renderer;
}

how to set a mapRect for mkOverlay?

i want set a rect on top of the polyline route on my map.
this is what exactly i'm trying to do:
- (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] colorWithAlphaComponent:0.7];
routeRenderer.lineWidth = 5.0;
[self.mapView.visibleMapRect = route.boundingMapRect];
return routeRenderer;
}
else return nil;
}
i have problem with this line of code :
[self.mapView.visibleMapRect = route.boundingMapRect];
i get the "Expected identifier" error. what is wrong with this line of code?
is that the correct way to set an Mkrect for an MKPolyline route?
thanks!
That's not how you write objective-C, try this
self.mapView.visibleMapRect = route.boundingMapRect;
or
[self.mapView setVisibleMapRect:route.boundingMapRect animated:YES];
i have solved with this tow line of code:
MKMapRect test = MKMapRectInset(route.boundingMapRect, -route.boundingMapRect.size.height/2, -route.boundingMapRect.size.width/2);
[self.mapView setVisibleMapRect:test animated:YES];

Overlay view cannot be removed from map

I have created some overlays:
//additionally draw an overlay
MKCircle *circle = [MKCircle circleWithCenterCoordinate:choosenCountry.coordinate radius:choosenCountry.placeMark.region.radius/4];
circle.title = #"test";
[_mapView addOverlay:circle];
and:
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay
{MKCircleView* circleView = [[MKCircleView alloc] initWithOverlay:overlay];
circleView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
circleView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
circleView.lineWidth = 2;
return circleView;}
But now, somehow I need to remove them but I can't:
- (void)clearOverlays{
NSArray *overlayCountries = [self.mapView overlays];
[self.mapView removeOverlays:overlayCountries];
}
Do you know, how this can be done? Thanks!

how to handle multiple MKOverlays in delegate function

I want to have a MKMapView with an two different overlays.
First, I have an "Image Overlay on the Map" (TileOverlay),
and secondly I want to draw a route as an overlay on the Map.
Everything works fine if I do this stuff in two different projects (One with the image overlay, and the other with the route overlay)
Now, I am wondering how the viewForOverlay delegate function should look like if I merge my projects?
For my Image (tile) overlay i currently looks like this:
- (MKOverlayView *) mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
TileOverlayView *tileView = [[TileOverlayView alloc] initWithOverlay:overlay];
tileView.tileAlpha = 1.0;
return tileView;
}
For my route Overlay it looks like this:
- (MKOverlayView*)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay];
polylineView.lineJoin = kCGLineJoinRound;
polylineView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.4];
return polylineView;
}
Now if i want to "merge" these (into one Project), how should this method look like?
- (MKOverlayView*)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
//what comes here?
}
You could deal with this situation by first checking the type of the overlay passed into your mapView:viewForOverlay: method, like this:
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {
if ([overlay isKindOfClass:[MKPolyline class]]) {
MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay];
polylineView.lineJoin = kCGLineJoinRound;
polylineView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.4];
return polylineView;
} else {
TileOverlayView *tileView = [[TileOverlayView alloc] initWithOverlay:overlay];
tileView.tileAlpha = 1.0;
return tileView;
}

Resources