Como exibir uma imagem em um MKOverlayView?
ATUALIZAR:
As imagens projetadas no MKMapView usando um MKOverlayView usam oProjeção Mercator, enquanto a imagem que eu uso como dados de entrada usa uma projeção WGS84. Existe uma maneira de converter a imagem de entrada, na projeção correta WGS84 -> Mercator, sem colocar a imagem em mosaico e isso pode ser feito em tempo real?
Normalmente você pode converter uma imagem na projeção correta usando o programagdal2tiles. Os dados de entrada, no entanto, são alterados a cada quinze minutos; portanto, a imagem deve ser convertida a cada quinze minutos. Portanto, a conversão deve ser feita em tempo real. Também quero que o lado a lado seja feito pelo Mapkit e não por mim mesmo usando gdal2tiles ou a estrutura GDAL.
UPDATE END
Atualmente, estou trabalhando em um projeto que exibe um radar de chuva em alguma parte do mundo. O radar i, mage é fornecido pelo EUMETSAT, eles oferecem um arquivo KML que pode ser carregado no Google Earth ou Google Maps. Se eu carregar o arquivo KML no Google Maps, ele será exibido perfeitamente, mas se eu desenhar a imagem usando um MKOverlayView em um MKMapView, a imagem será ligeiramente diferente.
Por exemplo, no lado esquerdo, no Google Maps e no lado direito, a mesma imagem é exibida em um MKMapView.
A superfície que a imagem cobre pode ser visualizada naGoogle Maps, o satélite usado para a imagem é o satélite "Meteosat 0 Degree".
A superfície que as duas imagens cobrem é do mesmo tamanho, é o LatLonBox do arquivo KML, especifica onde os lados superior, inferior, direito e esquerdo de uma caixa delimitadora da linha de sobreposição estão alinhados.
<LatLonBox id="GE_MET0D_VP-MPE-latlonbox">
<north>57.4922</north>
<south>-57.4922</south>
<east>57.4922</east>
<west>-57.4922</west>
<rotation>0</rotation>
</LatLonBox>
Crio um novo objeto MKOverlay personalizado chamado RadarOverlay com esses parâmetros,
[[RadarOverlay alloc] initWithImageData:[[self.currentRadarData objectAtIndex:0] valueForKey:@"Image"] withLowerLeftCoordinate:CLLocationCoordinate2DMake(-57.4922, -57.4922) withUpperRightCoordinate:CLLocationCoordinate2DMake(57.4922, 57.4922)];
A implementação do objeto MKOverlay personalizado; RadarOverlay
- (id) initWithImageData:(NSData*) imageData withLowerLeftCoordinate:(CLLocationCoordinate2D)lowerLeftCoordinate withUpperRightCoordinate:(CLLocationCoordinate2D)upperRightCoordinate
{
self.radarData = imageData;
MKMapPoint lowerLeft = MKMapPointForCoordinate(lowerLeftCoordinate);
MKMapPoint upperRight = MKMapPointForCoordinate(upperRightCoordinate);
mapRect = MKMapRectMake(lowerLeft.x, upperRight.y, upperRight.x - lowerLeft.x, lowerLeft.y - upperRight.y);
return self;
}
- (CLLocationCoordinate2D)coordinate
{
return MKCoordinateForMapPoint(MKMapPointMake(MKMapRectGetMidX(mapRect), MKMapRectGetMidY(mapRect)));
}
- (MKMapRect)boundingMapRect
{
return mapRect;
}
A implementação do MKOverlayView personalizado, RadarOverlayView
- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context
{
RadarOverlay* radarOverlay = (RadarOverlay*) self.overlay;
UIImage *image = [[UIImage alloc] initWithData:radarOverlay.radarData];
CGImageRef imageReference = image.CGImage;
MKMapRect theMapRect = [self.overlay boundingMapRect];
CGRect theRect = [self rectForMapRect:theMapRect];
CGRect clipRect = [self rectForMapRect:mapRect];
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
CGContextSetAlpha(context, [preferences floatForKey:@"RadarTransparency"]);
CGContextAddRect(context, clipRect);
CGContextClip(context);
CGContextDrawImage(context, theRect, imageReference);
[image release];
}
Quando faço o download da imagem, inverto a imagem para que ela possa ser facilmente desenhada no MKOverlayView
size_t width = (CGImageGetWidth(imageReference) / self.scaleFactor);
size_t height = (CGImageGetHeight(imageReference) / self.scaleFactor);
// Calculate colorspace for the specified image
CGColorSpaceRef imageColorSpace = CGImageGetColorSpace(imageReference);
// Allocate and clear memory for the data of the image
unsigned char *imageData = (unsigned char*) malloc(height * width * 4);
memset(imageData, 0, height * width * 4);
// Define the rect for the image
CGRect imageRect;
if(image.imageOrientation==UIImageOrientationUp || image.imageOrientation==UIImageOrientationDown)
imageRect = CGRectMake(0, 0, width, height);
else
imageRect = CGRectMake(0, 0, height, width);
// Create the imagecontext by defining the colorspace and the address of the location to store the data
CGContextRef imageContext = CGBitmapContextCreate(imageData, width, height, 8, width * 4, imageColorSpace, kCGImageAlphaPremultipliedLast);
CGContextSaveGState(imageContext);
// Scale the image to the opposite orientation so it can be easylier drawn with CGContectDrawImage
CGContextTranslateCTM(imageContext, 0, height);
CGContextScaleCTM(imageContext, 1.0, -1.0);
if(image.imageOrientation==UIImageOrientationLeft)
{
CGContextRotateCTM(imageContext, M_PI / 2);
CGContextTranslateCTM(imageContext, 0, -width);
}
else if(image.imageOrientation==UIImageOrientationRight)
{
CGContextRotateCTM(imageContext, - M_PI / 2);
CGContextTranslateCTM(imageContext, -height, 0);
}
else if(image.imageOrientation==UIImageOrientationDown)
{
CGContextTranslateCTM(imageContext, width, height);
CGContextRotateCTM(imageContext, M_PI);
}
// Draw the image in the context
CGContextDrawImage(imageContext, imageRect, imageReference);
CGContextRestoreGState(imageContext);
Depois de virar a imagem, manipulo-a e armazeno-a na memória como um objeto NSData.
Parece que a imagem foi esticada, mas parece bem no centro da imagem, que fica no equador.