J'utilise un MKOverlay personnalisé pour dessiner des données météorologiques sur un MKMapView. Le dessin est en cours dans CoreGraphics. Pour ce cas particulier, il n'est pas suffisant de faire le dessin dans la méthode drawMapRect: zoomScale: inContext: à cause de la façon dont il gère le pavage. J'ai besoin que l'image entière soit dessinée à la fois au lieu de mosaïque comme la méthode drawMapRect. Avant, j'avais l'image radar dans un fichier .gif, donc je viens de lui ajouter une imageView et de redimensionner l'imageView dans drawMapRect.Dessin sur mesure MKOverlay
Mon plan était de faire quelque chose de similaire avec ça. Ajoutez un UIView personnalisé et appelez setNeedsDisplay dans drawMapRect.
Voici le code approprié.
La propriété boundingMapRect de l'objet MKOverlay:
- (MKMapRect)boundingMapRect
{
CLLocationCoordinate2D upperLeftCoord =
CLLocationCoordinate2DMake(weatherData.radarArray.connectedRadar.latitude + 2.5,
weatherData.radarArray.connectedRadar.longitude - 2.5);
MKMapPoint upperLeft = MKMapPointForCoordinate(upperLeftCoord);
CLLocationCoordinate2D lowerRightCoord =
CLLocationCoordinate2DMake(weatherData.radarArray.connectedRadar.latitude - 2.5,
weatherData.radarArray.connectedRadar.longitude + 2.5);
MKMapPoint lowerRight = MKMapPointForCoordinate(lowerRightCoord);
double width = lowerRight.x - upperLeft.x;
double height = lowerRight.y - upperLeft.y;
MKMapRect bounds = MKMapRectMake(upperLeft.x, upperLeft.y, width, height);
return bounds;
}
Le drawMapRect de travail: zoomScale: inContext: code (qui est trop lent).
- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context {
int numPaths = parser.dataPaths.size();
// We have to pad the map rect a lot to allow for visibility testing that works well.
MKMapRect testMapRect = MKMapRectMake(mapRect.origin.x - 40000, mapRect.origin.y - 40000, mapRect.size.width + 40000, mapRect.size.height + 40000);;
// Only draw inside the area we are suppose to
//CGRect rect = [self rectForMapRect:mapRect];
//CGContextClipToRect(context, rect);
// How see through is the radar data. 1 = opaque, 0 = completely transparent
CGContextSetAlpha(context, 1);
for (int i = 0; i < numPaths; i++) {
// Make sure the bin is actually visible in this region before drawing it
if (MKMapRectContainsPoint(testMapRect, parser.dataPaths[i]->points[0]) ||
MKMapRectContainsPoint(testMapRect, parser.dataPaths[i]->points[1]) ||
MKMapRectContainsPoint(testMapRect, parser.dataPaths[i]->points[2]) ||
MKMapRectContainsPoint(testMapRect, parser.dataPaths[i]->points[3])) {
CGMutablePathRef path = CGPathCreateMutable();
CGPoint currentP = [self pointForMapPoint:parser.dataPaths[i]->points[0]];
CGContextBeginPath(context);
CGPathMoveToPoint(path, NULL, currentP.x, currentP.y);
currentP = [self pointForMapPoint:parser.dataPaths[i]->points[1]];
CGPathAddLineToPoint(path, NULL, currentP.x, currentP.y);
currentP = [self pointForMapPoint:parser.dataPaths[i]->points[2]];
CGPathAddLineToPoint(path, NULL, currentP.x, currentP.y);
currentP = [self pointForMapPoint:parser.dataPaths[i]->points[3]];
CGPathAddLineToPoint(path, NULL, currentP.x, currentP.y);
currentP = [self pointForMapPoint:parser.dataPaths[i]->points[0]];
CGPathAddLineToPoint(path, NULL, currentP.x, currentP.y);
CGPathCloseSubpath(path);
CGContextSetFillColorWithColor(context, colors[parser.dataPaths[i]->dataVal]);
CGContextAddPath(context, path);
CGContextFillPath(context);
CGPathRelease(path);
}
}
Le nouveau drawMapRect: zoomScale: inContext: Code
- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context {
// We have to pad the map rect a lot to allow for visibility testing that works well.
radarImageView.testMapRect = MKMapRectMake(mapRect.origin.x - 40000, mapRect.origin.y - 40000, mapRect.size.width + 40000, mapRect.size.height + 40000);
radarImageView.frame = [self rectForMapRect:self.overlay.boundingMapRect];
[radarImageView setNeedsDisplay];
}
La méthode drawRect du UIView personnalisé.
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
int numPaths = parser.dataPaths.size();
CGContextSetAlpha(context, 1);
for (int i = 0; i < numPaths; i++) {
// Make sure the bin is actually visible in this region before drawing it
if (MKMapRectContainsPoint(testMapRect, parser.dataPaths[i]->points[0]) ||
MKMapRectContainsPoint(testMapRect, parser.dataPaths[i]->points[1]) ||
MKMapRectContainsPoint(testMapRect, parser.dataPaths[i]->points[2]) ||
MKMapRectContainsPoint(testMapRect, parser.dataPaths[i]->points[3])) {
CGMutablePathRef path = CGPathCreateMutable();
CGPoint currentP = [(RadarImageOverlayView *)self.superview pointForMapPoint:parser.dataPaths[i]->points[0]];
CGContextBeginPath(context);
CGPathMoveToPoint(path, NULL, currentP.x, currentP.y);
currentP = [(RadarImageOverlayView *)self.superview pointForMapPoint:parser.dataPaths[i]->points[1]];
CGPathAddLineToPoint(path, NULL, currentP.x, currentP.y);
currentP = [(RadarImageOverlayView *)self.superview pointForMapPoint:parser.dataPaths[i]->points[2]];
CGPathAddLineToPoint(path, NULL, currentP.x, currentP.y);
currentP = [(RadarImageOverlayView *)self.superview pointForMapPoint:parser.dataPaths[i]->points[3]];
CGPathAddLineToPoint(path, NULL, currentP.x, currentP.y);
currentP = [(RadarImageOverlayView *)self.superview pointForMapPoint:parser.dataPaths[i]->points[0]];
CGPathAddLineToPoint(path, NULL, currentP.x, currentP.y);
CGPathCloseSubpath(path);
CGContextSetFillColorWithColor(context, colors[parser.dataPaths[i]->dataVal]);
CGContextAddPath(context, path);
CGContextFillPath(context);
CGPathRelease(path);
}
}
}
Merci!
EDIT
Je pense que le problème a quelque chose à voir avec le contexte de la RadarImageView. Y a-t-il un problème avec la façon dont j'obtiens le contexte dans la méthode drawRect: peut-être?
Ahh c'est une bonne idée. Je vais essayer et vous faire savoir comment cela fonctionne. –
J'ai essayé ceci et la performance n'était toujours pas assez bonne. Le nombre typique de chemins est d'environ 30 000. Il y a un chemin pour chaque point de données dans le domaine du radar (les zéros ne sont pas comptés). Ce nombre pourrait théoriquement atteindre 165 000. –
Quel est le nombre total de chemins? Quel est le nombre de chemins visibles? L'utilisateur peut-il changer le zoom? Pouvez-vous avoir tous les chemins visibles? – FKDev