2013-05-16 45 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

就個人而言,我喜歡的地圖包的方法,因爲它可以讓你出的手動計算屏幕座標的業務。但是如果你真的不想在那裏使用地圖背景,你可以手動完成。