如何从点数组创建CGPathRef [英] How to create CGPathRef from Array of points

查看:517
本文介绍了如何从点数组创建CGPathRef的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我一直在地图应用程序中工作,我需要在两个位置之间绘制路线,我也有路线坐标(使用google Direction api)并将其保存在一个数组中,现在我只需要要做的是从点数组创建一个路径,稍后我将使用MKOverlayPathView的路径在地图上创建真实路线。这里我的问题是如何从坐标数组创建一个CGPathRef,或者任何其他方式来执行相同的操作

在此先感谢

Hi all i've been working in a map application, Where i needed to draw the route between two locations, I've got route coordinates too (using google Direction api) and kept it in an array, Now all i need to do is creating a path from the array of points, later i will use the path with MKOverlayPathView for creating real routes on the map. Here my problem is how to create a CGPathRef from the array of coordinates, Or any other way to do the same operation
Thanks in Advance

推荐答案

假设坐标作为NSValue对象存储在NSArray中,您可以执行以下操作:

Assuming the coordinates are stored in an NSArray as NSValue objects, you can do the following:

CGMutablePathRef path = CGPathCreateMutable();
if (points && points.count > 0) {
    CGPoint p = [(NSValue *)[points objectAtIndex:0] CGPointValue];
    CGPathMoveToPoint(path, nil, p.x, p.y);
    for (int i = 1; i < points.count; i++) {
        p = [(NSValue *)[points objectAtIndex:i] CGPointValue];
        CGPathAddLineToPoint(path, nil, p.x, p.y);
    }
}
// do stuff
CGPathRelease(path);

这篇关于如何从点数组创建CGPathRef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆