2013-05-16 42 views
2

我有要求我必须在CLLocationManager中绘制区域的圆。我已经完成了要求与本准则的,如何在半径增加时在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; 

我已经离开了把作为enter image description here

问题是当我给出的半径值是100,我得到的结果是屏幕截图。如果我将价值定为200,那么显然这个圆圈会增加。

我的问题是,我想圆在相同大小在任何给定数值比如200,300或400。

+1

lol @ rajesh.k ..你可以硬编码在半径100 .. –

+0

圆将增加。下一行你说圆相同的大小..我不能unserstand :( – Rushabh

+0

当我增加半径它增加了circle.but我不想增加,我想循环为静态。 –

回答

0

我想,你希望当用户放大你的圈子,以增加和减小尺寸。这样,圆圈将永远在地图上的同一区域。

在这种情况下,使用MKCircle,使用+circleWithCenterCoordinate:radius:创建,符合MKMapViewDelegate并设置笔划并填写mapView:viewForOverlay:

2

你的问题是关于增加半径的问题,但在这个论坛中,“半径”通常被解释为以点/像素为单位测量的圆的半径。

显然,这不是你要求的,但。现在,你已经说过你不想使用MKMapView,但我认为在讨论你的目标时这是一种有用的语言。底线,你不想改变屏幕上显示的圆的半径。您想要更改将在前述圆圈中显示的地图的“地区”(MKCoordinateRegion)的“跨度”(MKCoordinateSpan)。

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

  1. 手动:

    • 定义了一些有用的特性:

      @property (nonatomic) MKCoordinateRegion region; 
      @property (nonatomic) CGRect frame; 
      

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

    国债收益率我们看起来像一个观点:

    manual

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

    • 你可以添加地图视图(见Location Awareness Programming Guide

    • 把你CAShapeLayer您创建,而非新增一个子层,你可以改为夹映射到圆形CAShapeLayer

      [self.mapView.layer setMask:circleShapeLayer]; 
      

    而且,一旦你已经做了所有注释的增加的观点,你得到的东西是这样的:

    map kit view

就个人而言,我喜欢的地图包的方法,因为它可以让你出的手动计算屏幕坐标的业务。但是如果你真的不想在那里使用地图背景,你可以手动完成。