我正在尝试创建一个自定义MKOverlayView
类来绘制叠加层,因为正常MKOverlay
和MKOverlayView
不符合我正在构建的应用程序的需求。
当我绘制多边形覆盖图时,我遇到了问题;看起来没有好像我让MKOverlayView
画出来的那样好,我的意思是边缘不锐利,当你放大地图时它们都是像素化的。此外,由于某种原因,点到点的线也不会被绘制出来。
此外,当放大一些多边形时,图形会被剪掉,直到我再次缩小。
这是我的抽奖代码
- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context{
MapOverlay *newOverlay = (MapOverlay *)self.overlay;
CGColorRef ref2 = [newOverlay.strokeColor CGColor];
CGContextSetLineWidth(context, newOverlay.lineWidth);
CGColorRef ref = [newOverlay.fillColor CGColor];
int _countComponents = CGColorGetNumberOfComponents(ref);
if (_countComponents == 4) {
const CGFloat *_components2 = CGColorGetComponents(ref);
CGFloat red = _components2[0];
CGFloat green = _components2[1];
CGFloat blue = _components2[2];
CGFloat alpha = _components2[3];
CGContextSetRGBFillColor(context, red, green, blue, alpha);
}
for (int i=0; i<newOverlay.coordinates.count; i++){
if(i % 2 == 0) {
CLLocationCoordinate2D p;
p.latitude = (CLLocationDegrees)[(NSString *)[newOverlay.coordinates objectAtIndex:i+1]floatValue];
p.longitude = (CLLocationDegrees)[(NSString *)[newOverlay.coordinates objectAtIndex:i] floatValue];
//CLLocation *p = [[CLLocation alloc] initWithLatitude:(CLLocationDegrees)[(NSString *)[newOverlay.coordinates objectAtIndex:i+1] floatValue] longitude:(CLLocationDegrees)[(NSString *)[newOverlay.coordinates objectAtIndex:i] floatValue]];
MKMapPoint point = MKMapPointForCoordinate(p);
CGPoint point2 = [self pointForMapPoint:point];
if (i==0){
CGContextMoveToPoint(context, point2.x, point2.y);
}else{
CGContextAddLineToPoint(context, point2.x, point2.y);
}
}
}
CGContextDrawPath(context, kCGPathFillStroke);
//CGContextStrokePath(context);
}
我发现很少有关于自定义MKOverlayView
类的信息以及如何在地图上绘制,但这些是我用来执行此操作的2个教程
http://spitzkoff.com/craig/?p=65
http://i.ndigo.com.br/2012/05/ios-maps-with-image-overlays/
更新
我觉得它可能与叠加层的边界框有关,因为如果我返回bounding box of the world
它显示正常,但显然没有效率来绘制所有可能的叠加。
以下是我在自定义MKOverlay类
中找到叠加层的边界框的方法- (MKMapRect)boundingMapRect{
double maxY = 0;
double minY = 0;
double maxX = 0;
double minX = 0;
for(NSUInteger i = 0;i < coordinates.count; i++) {
if(i % 2 == 0) {
CLLocationCoordinate2D tempLoc;
tempLoc.latitude =(CLLocationDegrees)[(NSString *)[coordinates objectAtIndex:(NSUInteger)i + 1] floatValue];
tempLoc.longitude = (CLLocationDegrees)[(NSString *)[coordinates objectAtIndex:(NSUInteger)i] floatValue];
MKMapPoint tempPoint = MKMapPointForCoordinate(tempLoc);
if(i == 0){
minX = tempPoint.x;
minY = tempPoint.y;
if(tempPoint.x > maxX){
maxX = tempPoint.x;
}
if (tempPoint.y > maxY){
maxY = tempPoint.y;
}
}else{
if(tempPoint.x > maxX){
maxX = tempPoint.x;
}
if(tempPoint.x < minX){
minX = tempPoint.x;
}
if (tempPoint.y > maxY){
maxY = tempPoint.y;
}
if(tempPoint.y < minY){
minY = tempPoint.y;
}
}
}
}
MKMapRect b2 = MKMapRectMake(minX, maxY, minX-maxX, minY-maxY);
return b2;
//return MKMapRectWorld;
}