我有一个要求,我必须在 CLLocationManager 中为区域绘制一个圆圈。我已经完成了本准则的要求,
CLLocationDegrees latitude = 37.33492222;
CLLocationDegrees longitude = -122.03304215;
CLLocationCoordinate2D centerPoint = CLLocationCoordinate2DMake(latitude, longitude);
CLLocationDistance radius = 100.0;
CLRegion *region = [[CLRegion alloc]initCircularRegionWithCenter:centerPoint radius:radius identifier:@"Apple"];
[locationManager startMonitoringForRegion:region];
CAShapeLayer *circle = [CAShapeLayer layer];
circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0*radius, 2.0*radius)
cornerRadius:radius].CGPath;
// Center the shape in self.view
circle.position = CGPointMake(CGRectGetMidX(self.view.frame)-radius,
CGRectGetMidY(self.view.frame)-radius);
// Configure the apperence of the circle
circle.fillColor = [UIColor clearColor].CGColor;
circle.strokeColor = [UIColor redColor].CGColor;
circle.lineWidth = 1;
circle.opacity = 0.5;
// Add to parent layer
[self.view.layer addSublayer:circle;
I have Got Out Put as
问题是当我将半径值指定为 100 时,我得到的结果为 ScreenShot。如果我将值指定为 200,那么显然圆将会增加。
我的问题是,当给定任何值(例如 200、300 或 400)时,我希望圆具有相同的大小。
您的问题涉及增加半径,但在本论坛中,“半径”通常被解释为以点/像素为单位测量的圆的半径。
显然,这不是您所要求的。现在,您已经说过您不想使用MKMapView
,但我认为在谈论你的目标时这是一种有用的语言。最重要的是,您不想更改屏幕上显示的圆的半径。您想要更改“跨度”(MKCoordinateSpan http://developer.apple.com/library/ios/documentation/MapKit/Reference/MapKitDataTypesReference/Reference/reference.html#//apple_ref/doc/uid/TP40009734-CH1-SW29)的“地区”(MKCoordinateRegion http://developer.apple.com/library/ios/documentation/MapKit/Reference/MapKitDataTypesReference/Reference/reference.html#//apple_ref/doc/uid/TP40009734-CH1-SW30)将在上述圆圈中呈现的地图。
有两种方法可以解决如何绘制代表地图投影的固定大小的圆的问题(但由于某种原因,您不需要地图)。
-
手动:
-
定义一些有用的属性:
@property (nonatomic) MKCoordinateRegion region;
@property (nonatomic) CGRect frame;
The region
定义将在用户界面中显示的纬度和经度范围。这frame
将是我们将在其中呈现这些纬度和经度点的屏幕坐标。请注意,要在用户界面中直观地表示纬度/经度坐标,您需要这两个方面,即定义可以表示的纬度/经度的内容以及说明将其放置在屏幕上的位置的其他内容。
-
所以,第一个问题是如何定义该区域。在这里,我定义了中心坐标并定义了距中心坐标 500 米的区域:
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(...);
self.region = MKCoordinateRegionMakeWithDistance(coordinate, 500.0, 500.0);
您问“如何更改所显示内容的半径[即跨度]?”您可以通过更改来做到这一点region
变量表示纬度/经度的范围将在用户界面中表示。
-
无论如何,您现在可以将圆圈添加到屏幕上:
- (void)addCircle
{
CGPoint center = CGPointMake(self.view.layer.bounds.size.width / 2.0, self.view.layer.bounds.size.height / 2.0);
CGFloat radius = self.view.layer.bounds.size.width * 0.40;
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center
radius:radius
startAngle:0.0
endAngle:M_PI * 2.0
clockwise:YES];
self.frame = CGRectMake(center.x - radius, center.y - radius, radius * 2.0, radius * 2.0);
CAShapeLayer *layer = [CAShapeLayer layer];
layer.path = [path CGPath];
layer.strokeColor = [[UIColor darkGrayColor] CGColor];
layer.fillColor = [[UIColor whiteColor] CGColor];
layer.lineWidth = 3.0;
[self.view.layer addSublayer:layer];
self.view.backgroundColor = [UIColor lightGrayColor];
}
-
编写一个例程来转换CLLocationCoordinate2D
到屏幕上的某个位置:
- (CGPoint)determineCGPointFromCoordinate:(CLLocationCoordinate2D)coordinate
{
CGFloat percentX = (coordinate.longitude - self.region.center.longitude + self.region.span.longitudeDelta / 2.0) / self.region.span.longitudeDelta;
CGFloat percentY = 1.0 - (coordinate.latitude - self.region.center.latitude + self.region.span.latitudeDelta / 2.0) / self.region.span.latitudeDelta;
return CGPointMake(self.frame.origin.x + percentX * self.frame.size.width,
self.frame.origin.y + percentY * self.frame.size.height);
}
-
然后您可以在屏幕上添加您的积分:
CGPoint center = [self determineCGPointFromCoordinate:coordinate];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"spot.png"]];
imageView.center = center;
[self.view addSubview:imageView];
这给我们带来了一个看起来像这样的视图:
-
在我看来,更容易的是重新评估您不使用地图视图的决定。
您可以添加地图视图(请参阅位置感知编程指南 http://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/MapKit/MapKit.html#//apple_ref/doc/uid/TP40009497-CH3-SW1)
-
带上你的CAShapeLayer
您创建的,而不是添加为子图层,您可以将地图剪辑到该圆形CAShapeLayer
:
[self.mapView.layer setMask:circleShapeLayer];
并且,一旦您完成了向视图添加所有注释,您将得到如下所示的内容:
就我个人而言,我喜欢地图套件方法,因为它使您摆脱了手动计算屏幕坐标的麻烦。但如果你真的不想要地图背景,你可以手动完成。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)