通过在iOS中获取经纬度来绘制视图中的土地形状?

时间:2013-07-19 09:30:31

标签: ios latitude-longitude drawrect

我想通过在陆地的角落取经纬度来绘制小地的形状。

我写了以下代码。现在我采用了硬核值。

- (void)drawRect:(CGRect)rect {

CGSize screenSize = [UIScreen mainScreen].applicationFrame.size;
SCALE = MIN(screenSize.width, screenSize.height) / (2.0 * EARTH_RADIUS);
OFFSET = MIN(screenSize.width, screenSize.height) / 2.0;

CGPoint latLong1 = {18.626103, 73.805023};
CGPoint latLong2 = {18.626444, 73.804884};
CGPoint latLong3 = {18.626226, 73.804969};
CGPoint latLong4 = {18.626103, 73.805023};

 NSMutableArray *points=[NSMutableArray arrayWithObjects:[NSValue valueWithCGPoint:[self convertLatLongCoord:latLong1]],[NSValue valueWithCGPoint:[self convertLatLongCoord:latLong2]], [NSValue valueWithCGPoint:[self convertLatLongCoord:latLong3]],[NSValue valueWithCGPoint:[self convertLatLongCoord:latLong4]],nil];

CGContextRef ctx = UIGraphicsGetCurrentContext();

   for(int i=0;i<points.count;i++)
   {

      // CGPoint newCoord = [self convertLatLongCoord:latLong];

       NSValue *val = [points objectAtIndex:i];
       CGPoint newCoord = [val CGPointValue];

       if(i == 0)
      {
          // move to the first point
          CGContextMoveToPoint(ctx, newCoord.x, newCoord.y);
      }
      else
      {
          CGContextAddLineToPoint(ctx, newCoord.x, newCoord.y);

          CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1);

          CGContextSetStrokeColorWithColor(ctx, [[UIColor redColor] CGColor]);
      }        
   } 
    CGContextStrokePath(ctx);
}

下面是将lat long转换为x,y坐标的方法。

- (CGPoint)convertLatLongCoord:(CGPoint)latLong 
  {

     CGFloat x = EARTH_RADIUS * cos(latLong.x) * cos(latLong.y) * SCALE + OFFSET;

     CGFloat y = EARTH_RADIUS * cos(latLong.x) * sin(latLong.y) * SCALE + OFFSET;

     return CGPointMake(x, y);

  }

我的问题是当我把小地(例如房屋地)区域拉长时,它的形状在画面后看不到。我怎样才能在视野中展现最大的土地形状。 提前致谢。

0 个答案:

没有答案