tags:

views:

125

answers:

0

I need to draw gird above mapkit view, and I want to bind grid to view. I have class Grid, that have array or grid elements.

//Cache class

-(MapCacheRegion)regionFrom:(CLLocationCoordinate2D)from to:(CLLocationCoordinate2D)to level:(int)level {
MapCacheRegion ret;
     float coef = 1;
     if (level>0) {
          coef = (baseBoxSize/(float)(level*levelScale));
     }
     ret.minX = (from.longitude+180)/coef;
     ret.minY = (90-from.latitude)/coef;
     ret.maxX = (to.longitude+180)/coef;
     ret.maxY = (90-to.latitude)/coef;
     return ret;
}

//Grid class
//This method draw grid above the map
- (void) drawRect: (CGRect)rect
{
        //topLeft is left top latitude of mapview, bottomRight is bottom-right :)
        //cacheRegion is struct that contains indexes of max and min element that be at map

MapCacheRegion cacheRegion = [cacheMap   regionFrom:topLeftto:bottomRightlevel:level];

     [[[UIColor whiteColor] colorWithAlphaComponent:0.5f] setStroke];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(ctx, 0.25f);

     float boxSize = 10.0f;//size of box in latitude and longitude

     CLLocationCoordinate2D leftTopBoxCoordinate;//it is position of left-top box
     if (cacheRegion.minX==0) {
          leftTopBoxCoordinate.longitude=-180.0f;
     } else {
          leftTopBoxCoordinate.longitude=-180.0f+(cacheRegion.minX-1)*boxSize;//
     }

     if (cacheRegion.minY==0) {
          leftTopBoxCoordinate.latitude=85.0f;
     } else {
          leftTopBoxCoordinate.latitude=85.0f-(cacheRegion.minY-1)*boxSize;
     }

        //Convert latitude and longitude to screen coordinates.
     CGPoint leftTopPoint = [map convertCoordinate:leftTopBoxCoordinate toPointToView:map];

     float boxDY=leftTopPoint.y;
     float boxDX=leftTopPoint.x;
     int rows = cacheRegion.maxY-cacheRegion.minY; // count of box
     int columns = cacheRegion.maxX-cacheRegion.minX; // count of box
     for (int row = 0; row < rows; row++)
     {
          for (int column = 0; column < columns; column++)
          {
UIColor *color = [[UIColor blueColor] colorWithAlphaComponent:0.5f];
                    [color setFill];

                    CGRect box = CGRectMake(column * kBoxWidth+boxDX,row * kBoxHeight+boxDY,kBoxWidth,kBoxHeight);
                    UIRectFill(box);
UIRectFrame(box);
                    NSString *str=[NSString stringWithFormat:@"%d:%d",cacheRegion.minY+row,cacheRegion.minX+column];
                       [[UIColor whiteColor] setStroke];
                       [[UIColor whiteColor] setFill];
                       CGRect rect = CGRectMake(box.origin.x+10, box.origin.y+10, 25, 25);
                    [str drawInRect:rect withFont :[ UIFontfontWithName:@"arial"size:10]];
          }
     }
}

The grid must move with map. Grid have size like 50x50 but on map user see only cells that contains in map region, 30x15 for sample. I have problem at this

CGPoint leftTopPoint = [map convertCoordinate:leftTopBoxCoordinate toPointToView:map];

From 85 degree and below after each 10 degree this method return bad data.

-[GridOverlay drawRect:]: 
L:85.000000,G:-180.000000
DX:0,DY:0
-[GridOverlay drawRect:]: 
L:80.058050,G:-180.000000
DX:0,DY:-56//good!
-[GridOverlay drawRect:]: 
L:79.432371,G:-180.000000
DX:0,DY:28//bad :(

That's happens and how fix it?