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;
Related
I have added a circle to MKMapView using below code in delegate function:
-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay{
MKCircleRenderer *circleView = [[MKCircleRenderer alloc] initWithOverlay:overlay];
circleView.strokeColor = [UIColor blueColor];
return circleView;
}
I want to reduce the thickness, can anyone help?
I found a solution for it, there is a property for increase/decrease line width in MKCircleRenderer class itself:
circleView.lineWidth = 2;
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;
}
I'm creating an Universal app and I have to create custom safe zones on the map view.
What I do is:
Add a new UIView as superview of map view called squareZone.
To the squareZone view I add UIPanGestureRecognizer, UIPinchGestureRecognizer and UIRotationGestureRecognizer so I can move, rotate and zoom (in and out).
Here is the code of SquareZone
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.opaque = NO;
self.layer.cornerRadius = 5.0f;
}
return self;
}
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
UIColor *color = [UIColor colorWithRed: 0.765 green: 0.439 blue: 0.443 alpha: 0.658];
UIBezierPath *rectanglePath = [UIBezierPath bezierPathWithRect: rect];
[color setFill];
[rectanglePath fill];
[UIColor.whiteColor setStroke];
rectanglePath.lineWidth = 5;
CGFloat rectanglePattern[] = {2, 3};
[rectanglePath setLineDash: rectanglePattern count: 2 phase: 0];
[rectanglePath stroke];
}
Now, when the user adjust the squareZone I have to show on a UILabel the distance between each point in meters. For that task I'm using
- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location
How can I add/show the four UILabels when the user interacts with the squareZone.
I need some light here. I had seen many tutorials but I cannot imagine how can this is posible. For reference, there is an app called
Trax: https://itunes.apple.com/us/app/trax-gps-tracker/id647170688?mt=8
I have to do the same Drawing Geofence Zone.
Thanks in advance.
First thing to mention is that Trax has two different modes:
1) editing -- where you draw the path
2) live -- where it shows the result.
Editing mode
While in editing, you cannot move and zoom inside of your MKMapView. This happens because they are using the same approach as you do -- they are adding a UIView on top of MKMapView, so that gestures do not conflict with each other.
All you need to do is add CGPoints to some array and use them in the future.
Of course, there are a few difficulties with using CoreGraphics framework, but it is not that tricky.
Live mode
After user added all CGPoints, you now have to convert these points into actual CLLocationCoordinate2D instances.
CGPoint thePoint = ...
CLLocationCoordinate2D theLocationCoordinate2D = [theMapView convertPoint:thePoint toCoordinateFromView:theDrawingView];
What Trax have in their app are probably (almost certainly) instances of MKPolygon class.
You can add it like so:
NSUInteger theCount = self.theMainDrawingView.thePointsArray.count;
CLLocationCoordinate2D theLocationCoordinatesArray[theCount];
for (NSUInteger theIndex = 0; theIndex < theCount; theIndex++)
{
CGPoint thePoint = self.theMainDrawingView.thePointsArray[theIndex].CGPointValue;
CLLocationCoordinate2D theLocationCoordinate2D = [self.theMainMapView convertPoint:thePoint toCoordinateFromView:self.theMainDrawingView];
theLocationCoordinatesArray[theIndex] = theLocationCoordinate2D;
}
MKPolygon *thePolygon = [MKPolygon polygonWithCoordinates:theLocationCoordinatesArray count:theCount];
[self.theMainMapView addOverlay:thePolygon];
However, this is not the end. This will trigger a delegate method of your MKMapView (don't forget to set its .delegate property)
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
if (![overlay isKindOfClass:[MKPolygon class]])
{
return nil;
}
MKPolygonView *thePolygonView = [[MKPolygonView alloc] initWithPolygon:(MKPolygon *)overlay];
thePolygonView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
thePolygonView.strokeColor = [[UIColor redColor] colorWithAlphaComponent:0.6];
thePolygonView.lineWidth = 4;
return thePolygonView;
}
Result looks like this:
Summary
Of course, this doesn't fully solve the issue, because you would have to add pinch and pan gestures too, but I hope that I could point into the right direction.
I tried following code to make circle:
MKCircle *circle = [MKCircle
circleWithCenterCoordinate:userLocation.coordinate radius:1000];
[map addOverlay:circle];
Then in Map View's delegate:
- (MKOverlayView *)mapView:(MKMapView *)map viewForOverlay:(id <MKOverlay>)overlay
{
MKCircleView *circleView = [[MKCircleView alloc] initWithOverlay:overlay];
circleView.strokeColor = [UIColor redColor];
circleView.fillColor = [[UIColor redColor] colorWithAlphaComponent:0.4];
return circleView;
}
It adds a circle around the pin, but how to make the circle stretchable like Reminder App's Location Reminder feature?
Look what I found:
https://github.com/d0ping/DBMapSelectorViewController
You can set up a lot of things like Radius of the circle, Background and Stroke color and even the Radius text.
I create new overlays like this:
MKCircle *circle = [MKCircle circleWithCenterCoordinate:region.coordinate radius:region.radius];
[self.mapView addOverlay:circle];
also I implemented delegate method:
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
MKCircleRenderer *circleRenderer = [[MKCircleRenderer alloc] init];
circleRenderer.fillColor = [UIColor greenColor];
circleRenderer.alpha = 1.f;
return circleRenderer;
}
both parts of code are called, mapView != nil at that moment, it's delegate set,
but I cannot see the circle on my map.
What am I doing wrong?
As per #Rob suggestion you need to init MKCircleRenderer using other method initWithCircle.
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
MKCircleRenderer *circleRenderer = [[MKCircleRenderer alloc] initWithCircle:overlay];
circleRenderer.fillColor = [UIColor greenColor];
circleRenderer.alpha = 1.f;
return circleRenderer;
}
Also make sure that fence distance is proper enough to visible the circle in map.
For example:
CLLocationDistance fenceDistance = 100000;
MKCircle *circle = [MKCircle circleWithCenterCoordinate:region.coordinate radius:fenceDistance];
[self.mapView addOverlay:circle];
Rather than init, call the MKCircleRenderer method initWithCircle.
Obviously, make sure the delegate of the map view is set, that your code that adds the overlay and that instantiates the renderer is called at all, etc., but initWithCircle is the likely culprit.