Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在MKMapSnapshot上绘制MKPolyline_Ios_Ios7_Mapkit - Fatal编程技术网

Ios 如何在MKMapSnapshot上绘制MKPolyline

Ios 如何在MKMapSnapshot上绘制MKPolyline,ios,ios7,mapkit,Ios,Ios7,Mapkit,在iOS 7中,您可以使用MapKit框架制作地图的静态快照。如果你想给某人发一封有地址或其他信息的电子邮件,这非常有用。我唯一缺少的是:如何在快照上绘制MKPolyline?这是如何: - (UIImage *)drawRoute:(MKPolyline *)polyline onSnapshot:(MKMapSnapshot *)snapShot withColor:(UIColor *)lineColor { UIGraphicsBeginImageContext(snapShot.im

在iOS 7中,您可以使用MapKit框架制作地图的静态快照。如果你想给某人发一封有地址或其他信息的电子邮件,这非常有用。我唯一缺少的是:如何在快照上绘制MKPolyline?

这是如何:

- (UIImage *)drawRoute:(MKPolyline *)polyline onSnapshot:(MKMapSnapshot *)snapShot withColor:(UIColor *)lineColor {

UIGraphicsBeginImageContext(snapShot.image.size);
CGRect rectForImage = CGRectMake(0, 0, snapShot.image.size.width, snapShot.image.size.height);

// Draw map
[snapShot.image drawInRect:rectForImage];

// Get points in the snapshot from the snapshot
int lastPointIndex;
int firstPointIndex = 0;
BOOL isfirstPoint = NO;
NSMutableArray *pointsToDraw = [NSMutableArray array];
for (int i = 0; i < polyline.pointCount; i++){
    MKMapPoint point = polyline.points[i];
    CLLocationCoordinate2D pointCoord = MKCoordinateForMapPoint(point);
    CGPoint pointInSnapshot = [snapShot pointForCoordinate:pointCoord];
    if (CGRectContainsPoint(rectForImage, pointInSnapshot)) {
        [pointsToDraw addObject:[NSValue valueWithCGPoint:pointInSnapshot]];
        lastPointIndex = i;
        if (i == 0)
            firstPointIndex = YES;
        if (!isfirstPoint) {
            isfirstPoint = YES;
            firstPointIndex = i;
        }
    }
}

// Adding the first point on the outside too so we have a nice path
    if (lastPointIndex+1 <= polyline.pointCount {
    MKMapPoint point = polyline.points[lastPointIndex+1];
    CLLocationCoordinate2D pointCoord = MKCoordinateForMapPoint(point);
    CGPoint pointInSnapshot = [snapShot pointForCoordinate:pointCoord];
    [pointsToDraw addObject:[NSValue valueWithCGPoint:pointInSnapshot]];
}
// Adding the point before the first point in the map as well (if needed) to have nice path

if (firstPointIndex != 0) {
    MKMapPoint point = polyline.points[firstPointIndex-1];
    CLLocationCoordinate2D pointCoord = MKCoordinateForMapPoint(point);
    CGPoint pointInSnapshot = [snapShot pointForCoordinate:pointCoord];
    [pointsToDraw insertObject:[NSValue valueWithCGPoint:pointInSnapshot] atIndex:0];
}

// Draw that points
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 3.0);

for (NSValue *point in pointsToDraw){
    CGPoint pointToDraw = [point CGPointValue];
    if ([pointsToDraw indexOfObject:point] == 0){
        CGContextMoveToPoint(context, pointToDraw.x, pointToDraw.y);
    } else {
        CGContextAddLineToPoint(context, pointToDraw.x, pointToDraw.y);
    }
}
CGContextSetStrokeColorWithColor(context, [lineColor CGColor]);
CGContextStrokePath(context);

UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultingImage;
}
-(UIImage*)绘图路线:(MKPolyline*)多段线快照:(MKMapSnapshot*)带颜色的快照:(UIColor*)线条颜色{
UIGraphicsBeginImageContext(snapShot.image.size);
CGRect rectForImage=CGRectMake(0,0,snapShot.image.size.width,snapShot.image.size.height);
//绘制地图
[snapShot.image drawInRect:rectForImage];
//从快照中获取快照中的点
int-lastPointIndex;
int firstPointIndex=0;
BOOL isfirstPoint=否;
NSMutableArray*pointsToDraw=[NSMutableArray];
对于(int i=0;i如果(lastPointIndex+1我将plaetzchen的答案转换为MKMapSnapshot的swift 4扩展名

import MapKit

extension MKMapSnapshot {

    func drawPolyline(_ polyline: MKPolyline, color: UIColor, lineWidth: CGFloat) -> UIImage {
        UIGraphicsBeginImageContext(self.image.size)
        let rectForImage = CGRect(x: 0, y: 0, width: self.image.size.width, height: self.image.size.height)

        // Draw map
        self.image.draw(in: rectForImage)

        var pointsToDraw = [CGPoint]()


        let points = polyline.points()
        var i = 0
        while (i < polyline.pointCount)  {
            let point = points[i]
            let pointCoord = MKCoordinateForMapPoint(point)
            let pointInSnapshot = self.point(for: pointCoord)
            pointsToDraw.append(pointInSnapshot)
            i += 1
        }

        let context = UIGraphicsGetCurrentContext()
        context!.setLineWidth(lineWidth)

        for point in pointsToDraw {
            if (point == pointsToDraw.first) {
                context!.move(to: point)
            } else {
                context!.addLine(to: point)
            }
        }

        context?.setStrokeColor(color.cgColor)
        context?.strokePath()

        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!
    }
}

我使用类似的方法创建保存到核心数据的路由图像:创建snapshop,基于快照的图形上下文,从lat生成CLLocationCoordinate2D并对每个核心数据点进行long,使用pointForCoordinate检查快照中的点是否存在,然后绘制。效果很好,但我仍在测试更复杂的路由-我将发布如果我发现你的答案有什么补充的话。
let image = mapSnapshot.drawPolyline(polyline, color: uiColor, lineWidth: 4)