Overlay view cannot be removed from map - ios

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!

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

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

Is CGPoint in MKPolygonView?

I'm trying to figure out a way to detect which MKOverlayView (actually MKPolygonView) was tapped and then change its color.
I got it running with this code:
- (void)mapTapped:(UITapGestureRecognizer *)recognizer {
MKMapView *mapView = (MKMapView *)recognizer.view;
MKPolygonView *tappedOverlay = nil;
for (id<MKOverlay> overlay in mapView.overlays)
{
MKPolygonView *view = (MKPolygonView *)[mapView viewForOverlay:overlay];
if (view){
// Get view frame rect in the mapView's coordinate system
CGRect viewFrameInMapView = [view.superview convertRect:view.frame toView:mapView];
// Get touch point in the mapView's coordinate system
CGPoint point = [recognizer locationInView:mapView];
// Check if the touch is within the view bounds
if (CGRectContainsPoint(viewFrameInMapView, point))
{
tappedOverlay = view;
break;
}
}
}
if([[tappedOverlay fillColor] isEqual:[[UIColor cyanColor] colorWithAlphaComponent:0.2]]){
[listOverlays addObject:tappedOverlay];
tappedOverlay.fillColor = [[UIColor redColor] colorWithAlphaComponent:0.2];
}
else{
[listOverlays removeObject:tappedOverlay];
tappedOverlay.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
}
//tappedOverlay.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
}
Which works but sometimes, depending where I tap it gets wrong which MKPolygonView was tapped. I suppose because CGRectContainsPoint doesnt calculate properly the area, since it's not a rectangle it's a Polygon.
What other methods there are to do this? I tried CGPathContainsPoint but I get worse results.
Thanks to #Ana Karenina, that pointed out the right way, this is how you have to convert the gesture so that the method CGPathContainsPoint' works right.
- (void)mapTapped:(UITapGestureRecognizer *)recognizer{
MKMapView *mapView = (MKMapView *)recognizer.view;
MKPolygonView *tappedOverlay = nil;
int i = 0;
for (id<MKOverlay> overlay in mapView.overlays)
{
MKPolygonView *view = (MKPolygonView *)[mapView viewForOverlay:overlay];
if (view){
CGPoint touchPoint = [recognizer locationInView:mapView];
CLLocationCoordinate2D touchMapCoordinate =
[mapView convertPoint:touchPoint toCoordinateFromView:mapView];
MKMapPoint mapPoint = MKMapPointForCoordinate(touchMapCoordinate);
CGPoint polygonViewPoint = [view pointForMapPoint:mapPoint];
if(CGPathContainsPoint(view.path, NULL, polygonViewPoint, NO)){
tappedOverlay = view;
tappedOverlay.tag = i;
break;
}
}
i++;
}
if([[tappedOverlay fillColor] isEqual:[[UIColor cyanColor] colorWithAlphaComponent:0.2]]){
[listOverlays addObject:tappedOverlay];
tappedOverlay.fillColor = [[UIColor redColor] colorWithAlphaComponent:0.2];
}
else{
[listOverlays removeObject:tappedOverlay];
tappedOverlay.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
}
//tappedOverlay.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
}

adding multiple circles around a pin in iOS

How can I add and display multiple circles in different colors inside a map (MKMapView)? I figured out how to add one circle, but can't figure out how to add multiple circles in various sizes and colors ... any help would be appreciated!
Here's some code I use to draw two concentric circles at a given location on the map. The outer one is gray, and the inner one is white. (in my example "range" is the circle radius) Both have some transparency:
- (void)drawRangeRings: (CLLocationCoordinate2D) where {
// first, I clear out any previous overlays:
[mapView removeOverlays: [mapView overlays]];
float range = [self.rangeCalc currentRange] / MILES_PER_METER;
MKCircle* outerCircle = [MKCircle circleWithCenterCoordinate: where radius: range];
outerCircle.title = #"Stretch Range";
MKCircle* innerCircle = [MKCircle circleWithCenterCoordinate: where radius: (range / 1.425f)];
innerCircle.title = #"Safe Range";
[mapView addOverlay: outerCircle];
[mapView addOverlay: innerCircle];
}
Then, make sure your class implements the MKMapViewDelegate protocol, and define how your overlays look in the following method:
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {
MKCircle* circle = overlay;
MKCircleView* circleView = [[MKCircleView alloc] initWithCircle: circle];
if ([circle.title compare: #"Safe Range"] == NSOrderedSame) {
circleView.fillColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.25];
circleView.strokeColor = [UIColor whiteColor];
} else {
circleView.fillColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.25];
circleView.strokeColor = [UIColor grayColor];
}
circleView.lineWidth = 2.0;
return circleView;
}
And, of course, don't forget to set the delegate on your MKMapView object, or the above method will never get called:
mapView.delegate = self;

Resources