Reputation: 455
I'm trying to render a circle at the center of a mapview. I'm also displaying the user's current location on the mapview, and setting the center of the map to there. These two are not lining up, though.
Here is where I set up my circle:
[self.mapView setShowsUserLocation:YES];
CAShapeLayer *circle = [CAShapeLayer layer];
int radius = 50;
circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0*radius, 2.0*radius)
cornerRadius:radius].CGPath;
// Set position of the circle.
// We use the frame of the mapview subtracted by circle radius to determine the center
circle.position = CGPointMake(CGRectGetMidX(self.mapView.frame)-radius,
CGRectGetMidY(self.mapView.frame)-radius);
circle.fillColor = [[UIColor alloc] initWithRed:0.5 green:0.5 blue:0.6 alpha:0.3].CGColor;
circle.strokeColor = [UIColor blackColor].CGColor;
circle.lineWidth = 1;
// Add to parent layer
[self.mapView.layer addSublayer:circle];
// Add 'crosshair'
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor blackColor];
view.frame = CGRectMake(0, 0, 5, 5);
[self.mapView addSubview:view];
view.center = self.mapView.center;
And in the didUpdateUserLocation delegate method I have:
[self.mapView setCenterCoordinate:userLocation.coordinate];
Here's what it looks like on the simulator.
I eventually want to allow the user to pan and select a region of the map, but now I'm not sure if my centercoordinate of the map is even coinciding with the center of my circle. Any help? Thanks!
Upvotes: 4
Views: 384
Reputation: 2073
Using Xamarin.Forms, I had to make sure to enable the Safe Area Layout Guide, adding this to my ContentPage
XAML.
<ContentPage ...
xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
Title="Safe Area"
ios:Page.UseSafeArea="true">
<StackLayout>
...
</StackLayout>
For more info: Safe Area Layout Guide on iOS
Upvotes: 0
Reputation: 7961
You may be running into the same bug I discovered recently. I have reported it to Apple and it was marked as a duplicate, so perhaps it'll be fixed in iOS 7.1.
Bug with MKMapView's centerCoordinate during rotation?
Upvotes: 1
Reputation: 13
I think It's a status bar issue.
Try this.
[self.mapView setShowsUserLocation:YES];
CAShapeLayer *circle = [CAShapeLayer layer];
int radius = 50;
circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0*radius, 2.0*radius)
cornerRadius:radius].CGPath;
// Set position of the circle.
// We use the frame of the mapview subtracted by circle radius to determine the center
circle.position = CGPointMake(CGRectGetMidX(self.mapView.frame)-radius,
CGRectGetMidY(self.mapView.frame)-radius-20);
circle.fillColor = [[UIColor alloc] initWithRed:0.5 green:0.5 blue:0.6 alpha:0.3].CGColor;
circle.strokeColor = [UIColor blackColor].CGColor;
circle.lineWidth = 1;
// Add to parent layer
[self.mapView.layer addSublayer:circle];
// Add 'crosshair'
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor blackColor];
view.frame = CGRectMake(0, 0, 5, 5);
[self.mapView addSubview:view];
view.center = self.mapView.center;
I hope it works with you.
Upvotes: 1
Reputation: 113777
Looks like a 20 point offset (same as the status bar). Perhaps your map view is being resized after you use the frame
to find the center.
You might try using an MKCircleView
or other overlays to do this instead of shape layers.
Upvotes: 0