how to handle multiple MKOverlays in delegate function - ios

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;
}

Related

How to create a circle overlay over my annotation in Maps using MApkit in iOS?

I want to create a circle overlay over the annotation. I'm using swift 3.0. Any help is appreciated !!
Try a custom overlay. Add this in viewDidLoad:
MKCircle *circle = [MKCircle circleWithCenterCoordinate:userLocation.coordinate radius:1000];
[map addOverlay:circle];
userLocation can be obtained by storing the MKUserLocationAnnotation as a property. Then, to actually draw the circle, put this in the map view's delegate:
- (MKOverlayRenderer *)mapView:(MKMapView *)map viewForOverlay:(id <MKOverlay>)overlay
{
MKCircleRenderer *circleView = [[MKCircleRenderer alloc] initWithOverlay:overlay];
circleView.strokeColor = [UIColor redColor];
circleView.fillColor = [[UIColor redColor] colorWithAlphaComponent:0.4];
return circleView;
}

How can I add shadow or edge to MKPolylineRenderer?

- (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;
}

'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;
}

does not display all the overlays, generating from kml file

I am drawing overlay on map from kml file which i store in my documents directory. i am viewing about 30 to 40 kml file at once with the line color stored itself in the kml. the problem is some of them doesn't display.
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
return [kmlParser viewForOverlay:overlay];
}
above code is from the reference KMLViewer which can be downloaded from here
when i write the below code it works perfectly fine but all the generated kml file draws with the black color
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
MKOverlayPathView *overlayPathView;
if ([overlay isKindOfClass:[MKPolygon class]])
{
overlayPathView = [[MKPolygonView alloc] initWithPolygon:(MKPolygon*)overlay];
overlayPathView.fillColor = [[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0] colorWithAlphaComponent:0.2];
overlayPathView.strokeColor = [[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0] colorWithAlphaComponent:0.7];
overlayPathView.lineWidth = 3;
return overlayPathView;
}
}
Any help would be greatly Appreciable!
Thanks.
boundingMapRect might be the culprit please check the zoom level of map as well as its boundingmaprect property

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];

Resources