Iphone 当半径增加时,如何在CLLocationManager中为区域绘制固定圆

Iphone 当半径增加时,如何在CLLocationManager中为区域绘制固定圆,iphone,ios,Iphone,Ios,我的要求是我必须在CLLocationManager中为区域画一个圆圈。我已经完成了此代码的要求 CLLocationDegrees latitude = 37.33492222; CLLocationDegrees longitude = -122.03304215; CLLocationCoordinate2D centerPoint = CLLocationCoordinate2DMake(latitude, longitude); CLLocationDistan

我的要求是我必须在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;
我已经出狱了

问题是当我将半径值设为100时,我得到的结果是屏幕截图。如果我给的值是200,那么很明显,圆圈会增加


我的问题是,当给定任何值,比如200、300或400时,我希望圆的大小相同

我假设您希望随着用户的缩放,圆的大小会增加或减小。这样,圆圈将始终位于地图上相同的土地区域上


在这种情况下,使用
MKCircle
,使用
+circleWithCenterCoordinate:radius:
创建,符合
MKMapViewDelegate
并设置笔划和填充
mapView:viewForOverlay:
您的问题涉及增加半径,但在本论坛中,“半径”通常将被解释为以点/像素为单位测量的圆半径

显然,这不是你想要的。现在,您已经说过不想使用
MKMapView
,但我认为在谈论您的目标时,它是一种有用的语言。总之,您不想更改屏幕上显示的圆的半径。您希望更改地图“区域”(region)的“跨度”(span),该区域将显示在前面的圆圈中

有两种方法可以解决这个问题,即如何绘制一个表示地图投影的固定大小的圆(但由于某种原因,您不需要地图)

  • 手动:

    • 定义一些有用的属性:

      @property (nonatomic) MKCoordinateRegion region;
      @property (nonatomic) CGRect frame;
      
      区域定义将在用户界面中显示的纬度和经度范围。
      将是屏幕坐标,我们将在其中显示这些纬度和经度点。注意,要在用户界面中直观地表示lat/long坐标,您需要这两个方面,一个是定义可以表示什么lat/long,另一个是说明在屏幕上放置它的位置

    • 因此,第一个问题是如何定义区域。这里我定义了中心坐标,并将一个区域定义为距中心500米:

      CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(...);
      self.region = MKCoordinateRegionMakeWithDistance(coordinate, 500.0, 500.0);
      
      您询问“如何更改所显示内容的半径[即跨度]”,您可以通过更改
      region
      变量来执行此操作,该变量表示将在UI中表示lat/long的范围

    • 无论如何,您现在可以将圆圈添加到屏幕:

      - (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];
      }
      
      CGPoint center = [self determineCGPointFromCoordinate:coordinate];
      UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"spot.png"]];
      imageView.center = center;
      [self.view addSubview:imageView];
      
    • 编写一个例程,将
      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);
      }
      
    • 然后,您可以在屏幕上添加您的点:

      - (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];
      }
      
      CGPoint center = [self determineCGPointFromCoordinate:coordinate];
      UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"spot.png"]];
      imageView.center = center;
      [self.view addSubview:imageView];
      
    这给我们提供了一个如下视图:

  • 在我看来,更容易的是重新评估不使用地图视图的决定

    • 您可以添加地图视图(请参见)

    • 以您创建的
      CAShapeLayer
      为例,您可以将地图剪辑到该圆形
      CAShapeLayer
      ,而不是添加为子层:

      [self.mapView.layer setMask:circleShapeLayer];
      
    并且,一旦您完成了向视图添加注释的所有操作,您会得到如下结果:


  • 就我个人而言,我喜欢MapKit方法,因为它使您摆脱了手动计算屏幕坐标的工作。但是如果你真的不想让地图背景出现在那里,你可以手动操作。

    lol@rajesh.k。。你可以在半径上硬编码100。圆圈将增大。下一行你说圆圈大小相同。。我受不了:(当我增加半径时,它是增加的圆。但我不想增加。我希望圆是静态的。@rajesh.k我知道你是堆栈溢出新手,所以我希望你不要介意一个建议:你现在已经问了六个关于堆栈溢出的问题,但没有接受一个答案。请回顾你以前的问题,检查是否有任何答案是wort如果被接受,请单击该答案旁边的大复选标记。