3

我敢肯定这是一个数学问题的多了,但我会词组它的UIView和上下文的iPad相关的目标CUIView的坐标系 - 抵消和翻译

我输入原始数据从我从其他地方下载的一些公共领域材料创建的地图文件中,然后拆分出来以隔离地图内的各个区域。每个地区都有一些次地区,就像美国大陆,然后是美国境内的各个州,然后每个次区域再次分解成县,也就是县。

每个州和每个县都有一个边界框,它告诉我每个的原点,宽度和高度。

在我的初始设置中,我为每个州创建了一个单独的视图,然后为每个县创建了另一个视图。代表州/县面积的多边形相对于通过接口构建器创建的视图(称为mainContainerView)进行了渲染(显然,该州位于州的顶部,因此它将可见)。此初始设置正确运行。

现在我试图通过将状态添加到持有该状态的多边形的UIView中来改变一些事情,这样我就可以将状态覆盖为县上的剪贴蒙版。问题在于,无论我尝试什么,我都无法让该县在国家的观点中转化为正确的地方。

它似乎应该是直接的加法或减法,因为每个项目的缩放比例是完全相同的,我不想做任何重大转换,所以我不相信CFAffineTransformation系列是必需的。

如果需要,我可以发布代码,但我不想让别人为我写程序;我只想让某人在这里指出我的正确方向,就如何在国家的角度设置州相对于州而言给我一个建议。

根据请求,这是我现在正在处理的相关代码。这段代码不起作用,但它可以让你知道我在做什么。发布示例数据有点难度,因为它涉及从.SHP文件中提取的点和数据的数组,以生成映射(和细分)。我将在代码中包含一些带有一些真实点值的注释,当我逐步完成该程序以向您展示正在发生的事情时。

MASK_MAX_EASTING,MASK_MAX_NORTHING,MASK_MIN_ETING和MASK_MIN_NORTHING是常数,它们定义了由状态组成的整个国家地图的边界框。

DIST_MAX_EASTING,DIST_MAX_NORTHING,DIST_MIN_ETING和DIST_MIN_NORTHING是定义由县组成的国家地图的边界框的常量。两幅地图的比例略有不同,因此,通过使用不同的边界框,我可以将两幅地图缩放到相同的大小。

-(void)didLoadMap:(NSNotification *)notification { 

id region = [notification object]; 
ShapePolyline *polygon = [region polygon]; 

if ([notification name] == @"MapsLoadingForState") { 

    // m_nBoundingBox is an array which contains the RAW northing and easting values for each subdivision. [0] - west limit, [1] - south limit, [2] - east limit, [3] - north limit. 

    // The code below, combined with the drawrect method in DrawMap.m (below) puts all the states on the map in precisely the right places, so for the state maps, it works just fine. 

    CGFloat originX = ((polygon->m_nBoundingBox[0]-MASK_MIN_EASTING)*stateScaleMultiplier)+([mainContainerView frame].size.width/2); 

    CGFloat originY = ((MASK_MAX_NORTHING-(polygon->m_nBoundingBox[3]))*stateScaleMultiplier)+[mainContainerView frame].origin.y; 
    CGFloat width = polygon->m_nBoundingBox[2] - polygon->m_nBoundingBox[0]; 
    CGFloat height = polygon->m_nBoundingBox[3] - polygon->m_nBoundingBox[1]; 

    CGFloat scaledWidth = width*stateScaleMultiplier; 
    CGFloat scaledHeight = height*stateScaleMultiplier; 
    UIColor *subViewColor = [UIColor colorWithRed:0.0 green:1.0 blue:1.0 alpha:0.0]; 

    stateMapView = [[DrawMap alloc] initWithFrame:CGRectMake(originX, originY, scaledWidth, scaledHeight)]; 
    [stateMapView setBackgroundColor:subViewColor]; 

    [stateMapView setStateScale:stateScaleMultiplier]; 
    [stateMapView setCountyScale:countyScaleMultiplier]; // Not actually needed. 

    [stateMapView setClippingMask:polygon]; 
    UIColor *colorMask = [UIColor colorWithWhite:1.0 alpha:1.0]; 
    [stateMapView setForeground:colorMask]; 

    [states addObject:stateMapView];      // Add the state map view to an array (for future use) 
    [mapView addSubview:stateMapView]; // MapView is a UIView of equivalent size and shape as mainContainerView. 

} else { 

    // This is where the problems occur. 

    CGFloat originX = (polygon->m_nBoundingBox[0]-DIST_MIN_EASTING); // 4431590 (raw data) 
    originX *= countyScaleMultiplier; // 303.929108 
    originX += ([mainContainerView frame].size.width/2); // 815.929077 

    CGFloat originY = (DIST_MAX_NORTHING-polygon->m_nBoundingBox[3]); 4328997 
    originY *= countyScaleMultiplier; // 296.893036 
    originY -= [mainContainerView frame].origin.y; // 340.893036 

    CGRect frame = [stateMapView frame]; // Dummy variable created for watches in the debugger. x=856.237183, y=332.169922 width=34.3800087, height=28.7534008 

    // When I was invoking DrawMap.h and the included drawrect method, the county map would easily be displayed in the right place, as you can see by the values above. 

    // This is where I think the problem is. The X value is WAY off as far as I can tell. 
    originX -= frame.origin.x; // -40.3081055 
    originY -= frame.origin.y; // 8.72311401 

    CGPoint countyOrigin = CGPointMake(originX,originY); 

    // Translate the county's origin so it is relative to the origin of stateMapView, not MainContainerView (doesn't work) 

    [stateMapView addCountyMap:[region polygon] withColor:winner translatedBy:countyOrigin]; 
    [stateMapView setNeedsDisplay]; 
} 

据我所知,有几个问题与此代码,这个问题的范围之外的一些东西,可以做一些你提出一个眉毛(或两个),但是这绝对是一项正在进行的工作.. 。

下面是来自DrawMap.m的相关代码;我已经剪掉了一堆东西,因为它是无关紧要的。

- (void)drawRect:(CGRect)rect { 

// Set up 

for (int i=0;i<[countyMaps count];i++) { 

    // Draw the polygon. 

    [[countyColors objectAtIndex:i] setFill]; 

    [self drawPolygon:[countyMaps objectAtIndex:i] 
      usingScale:stateScale 
     translatedBy:CGPointMake([[countyTranslations objectAtIndex:2*i] floatValue], 
            [[countyTranslations objectAtIndex:2*i+1] floatValue])]; 

} 

// Set the blend mode to multiply 

CGContextSetBlendMode(context, kCGBlendModeMultiply); 

// Draw a path with clippingMask 

[[UIColor colorWithWhite:1.0 alpha:1.0] setFill]; 
// CGPoint translate = CGPointMake(0,0); 

[self drawPolygon:clippingMask usingScale:stateScale translatedBy:CGPointMake(0,0)]; 

} 

-(void)drawPolygon:(ShapePolyline *)aPolygon usingScale:(float)mapScale translatedBy:(CGPoint)trans { 

for (int j=0;j<[aPolygon numParts];j++) { 

    UIBezierPath *path = [UIBezierPath bezierPath]; 

    [path setLineJoinStyle:kCGLineJoinRound]; 

    int startIndex = [[[aPolygon m_Parts] objectAtIndex:j] intValue]; 
    int endIndex = [aPolygon numPoints]; 

    CGPoint startPoint; 
    [[[aPolygon m_Points] objectAtIndex:startIndex] getValue:&startPoint]; 

    startPoint.x *=mapScale; 
    startPoint.y *=mapScale; 

    startPoint.x -= trans.x; 
    startPoint.y -= trans.y; 

    [path moveToPoint:startPoint]; 


    if (j+1 != [aPolygon numParts]){ 

     endIndex = [[[aPolygon m_Parts] objectAtIndex:j+1] intValue]; 
    } 

    for (int k=startIndex+1; k<endIndex; k++) 
    { 
     CGPoint nextPoint; 
     [[[aPolygon m_Points] objectAtIndex:k] getValue:&nextPoint]; 

     nextPoint.x *= mapScale; 
     nextPoint.y *= mapScale; 

     nextPoint.x -= trans.x; 
     nextPoint.y -= trans.y; 

     [path addLineToPoint:nextPoint]; 

    } 
    [path closePath]; 
    // [path stroke]; 
    [path fill]; 
} 

} 

本大部头真的可能是太多的信息,或者它可能是不够的。无论哪种方式,希望通过添加代码,我已经给你一些信息继续...

+0

请发布试图定位视图的代码以及一些示例数据。在我们弄清楚什么是错误之前,我们需要看看发生了什么。 – ughoavgfhw 2011-06-11 23:22:34

+0

根据ughoavgfhw的请求添加代码。 – 2011-06-12 04:32:13

+0

只是一个猜测......但'[stateMapView addCountyMap:[region polygon] withColor:winner translatedBy:countyOrigin];'这是期待的原点的逆? – 2011-06-12 07:00:49

回答

1

-SOLVED-

它很简单。我很惊讶,花了我很长时间才弄明白,因为我在最初的问题中是正确的 - 这是简单的加法和减法:

所有翻译现在都在呈现多边形的方法内完成。对于多边形中的每个点,我需要添加状态视图的原点,然后减去县的边界框的原点,然后从Y值(控制条的高度)中减去44。

我认为,这是一个过度思考问题,沮丧,过度思考的例子,三天后才发现答案是盯着你,脸上挂着一面红旗,喊,“我在这里!!!!”