2013-06-12 80 views
0

我正在使用MKOverlayMKOverlayView覆蓋MKMapView。要開始的事情,我只是想根據當前縮放級別將世界劃分爲瓦片。所以當我縮小時,我只想要一個大的瓷磚,下一個縮放級別4的瓷磚,然後是9等。這是工作。現在到我的問題:將MKMap分解成瓷磚 - 瓷磚太小

在zoomLevel 3,瓷磚之間開始有差距。這在縮放級別2上不會發生,只有4個圖塊,但在每個縮放級別中都是如此。

​​3210

這兩幅圖分別顯示了瓷磚1,2,4,5和5,6,8,9。

enter image description hereenter image description here

正如你可以看到每塊的後間隙增大。現在,我的代碼:

drawMapRect:

- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context 
{ 
    NSUInteger zoomLevel = [self zoomLevelForZoomScale:zoomScale]; 
    HeatMap *heatMap = (HeatMap *)self.overlay; 
    HeatMapTileManager *tileManager = [heatMap getHeatMapTileManagerForZoomLevel:zoomLevel]; 
    MKMapRect mapTile = [tileManager getRectForMapPoint:mapRect.origin atZoomLevel:zoomLevel]; 

    CGContextSetAlpha(context, 0.5); 
    CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();  
    CGColorRef color = CGColorCreate(rgb, (CGFloat[]){ .745, .941, .467, 1 }); 
    CGContextSetFillColorWithColor(context, color); 
    CGRect mapTileCGRect = [self rectForMapRect:mapTile]; 
    CGContextFillRect(context, mapTileCGRect); 
} 

getRectForMapPoint:

- (MKMapRect) getRectForMapPoint:(MKMapPoint)mapPoint 
         atZoomLevel:(NSInteger)level 
{   
    double stepSize = MKMapSizeWorld.width/(double)level; 

    double rectIDx = floor(mapPoint.x/stepSize); 
    double rectIDy = floor(mapPoint.y/stepSize); 

    MKMapRect mapRect = MKMapRectMake(stepSize * rectIDx, 
             stepSize * rectIDy, 
             stepSize, 
             stepSize); 

    NSLog(@"X: %f, Width: %f", mapRect.origin.x, stepSize); 
    NSLog(@"Y: %f, Height: %f", mapRect.origin.y, stepSize); 

    return mapRect; 
} 

回答

1

平鋪的常見的方式是每瓦分成4。所以頂層有1瓦。把它切成4,然後你有下一層。將其中的每一個都切割成4塊,然後你有下一層等等。這樣,任何圖層上的圖塊都可以很容易地計算出圖塊上下的圖塊的邊界,並且如果圖層的數據尚未準備好,可以使用上面的瓷磚的一部分,或者加入下面的四個瓷磚並使用它。

+0

謝謝,這是一個非常好的主意,我會改變這一點。但那現在不能解決我的問題嗎? – imbaer

+0

嗯,我不知道爲什麼,但是在我改變了平鋪之後,事情看起來和預期的一樣。這對我來說沒有意義,但我不會抱怨;)。 – imbaer