半径增加时如何在CLLocationManager中为区域绘制固定圆 [英] How to Draw Fixed Circle for Region in CLLocationManager when Radius Increased

查看:47
本文介绍了半径增加时如何在CLLocationManager中为区域绘制固定圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有要求我必须在CLLocationManager中为区域绘制一个圆.我已经按照以下代码完成了该要求,

  CLLocationDegrees纬度= 37.33492222;CLLocationDegrees经度= -122.03304215;CLLocationCoordinate2D centerPoint = CLLocationCoordinate2DMake(纬度,经度);CLLocationDistance半径= 100.0;CLRegion * region = [[CLRegion alloc] initCircularRegionWithCenter:centerPoint radius:radius identifier:@"Apple"];[locationManager startMonitoringForRegion:region];CAShapeLayer *圆= [CAShapeLayer层];circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0,0,2.0 * radius,2.0 * radius)cornerRadius:radius] .CGPath;//在self.view中将形状居中circle.position = CGPointMake(CGRectGetMidX(self.view.frame)-radius,CGRectGetMidY(self.view.frame)-radius);//配置圆的外观circle.fillColor = [UIColor clearColor] .CGColor;circle.strokeColor = [UIColor redColor] .CGColor;circle.lineWidth = 1;circle.opacity = 0.5;//添加到父层[self.view.layer addSublayer:circle; 

我已将内容发布为

问题是当我给定半径值为100时,我得到的结果为ScreenShot.如果我将值设为200,则显然圈子将增加.

我的问题是,当给定任何值(例如200、300或400)时,我希望圆具有相同的大小.

解决方案

您的问题是关于增加半径的,但是在本论坛中,半径"通常被解释为以点/像素为单位的圆的半径.

很明显,这不是您要的.现在,您已经说过您不想使用 MKMapView ,但是我认为这是谈论目标时使用的一种有用的语言.底线,您不想更改屏幕上显示的圆的半径.您要更改"span"()

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

      [self.mapView.layer setMask:circleShapeLayer]; 

  • 然后,在将所有注释添加到视图中之后,您将获得如下内容:

    我个人喜欢地图工具包的方法,因为它使您摆脱了手动计算屏幕坐标的麻烦.但是,如果您确实不希望在那里有地图背景,则可以手动进行.

    I have Requirement Were i have to Draw a Circle for Region in CLLocationManager. i have Accomplished the Requirement with this Code as,

    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

    Problem is When i given the radius value as 100, i got result as ScreenShot. if i will give value as 200 then obviously the Circle Will Increase.

    My Question is, I want the Circle as in same Size When any Value is Given say 200, 300 or 400.

    解决方案

    Your question talks about increasing the radius, but in this forum, "radius" will generally be construed as the radius of the circle measured in points/pixels.

    Clearly, that's not what you're asking for, though. Now, you've said you don't want to use a MKMapView, but I think it's a useful language to use when talking about your goals. Bottom line, you don't want to change the radius of the circle presented on the screen. You want to change the "span" (MKCoordinateSpan) of the "region" (MKCoordinateRegion) of the map which will be presented in the aforementioned circle.

    There are two ways of approaching this problem of how to draw a circle of fixed size that represents a projection of a map (but you don't, for some reason, want a map).

    1. Manually:

      • Define a few useful properties:

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

        The region defines the range of latitude and longitudes that will be presented in your user interface. The frame will be the screen coordinates in which we'll be presenting the those latitude and longitude points. Note, to do a visual representation of lat/long coordinates in a user interface you need both of these two aspects, something that defines what lat/long can be represented and something else that says where to put it on the screen.

      • So, the first question is how you define the region. Here I'm defining the center coordinate and defining a region as being 500 meters from that:

        CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(...);
        self.region = MKCoordinateRegionMakeWithDistance(coordinate, 500.0, 500.0);
        

        You asked "how do I change the radius [i.e. the span] of what is shown?" You do that by changing the region variable which says that range of lat/long will be represented in the UI.

      • Anyway, you can now you can add the circle to your screen:

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

      • Write a routine to convert from a CLLocationCoordinate2D to a location on the screen:

        - (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);
        }
        

      • You can then add your points on your screen:

        CGPoint center = [self determineCGPointFromCoordinate:coordinate];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"spot.png"]];
        imageView.center = center;
        [self.view addSubview:imageView];
        

      That yields us a view that looks like:

    2. Even easier, in my opinion, is to reevaluate your decision to not to use a map view.

      • You could add a map view (see the Location Awareness Programming Guide)

      • Take your CAShapeLayer that you created, and rather than adding as a sublayer, you could instead clip the map to that circular CAShapeLayer:

        [self.mapView.layer setMask:circleShapeLayer];
        

      And, once you've done all of the adding of annotations to the view, you get something like this:

    Personally, I like the map kit approach, as it gets you out of the business of manually calculating screen coordinates. But if you really don't want the map background there, you can do it manually.

    这篇关于半径增加时如何在CLLocationManager中为区域绘制固定圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆