NJCircleLine
想要绘制类似于谷歌地图中的点线吗?使用NJCircleLine!:-)
这个库为您提供了简单的接口来绘制路由导航类似的点线。它不仅可以绘制带有点集合的直线,还可以代表您调用谷歌方向API,并在提供可视化配置文件的情况下,绘制返回的路线点线。此库还提供了接口,以在地图相机缩放变化时重绘/调整线条大小。
示例
要运行示例项目,请克隆仓库,然后首先从示例目录中运行pod install
。
要求
由于这个库依赖于谷歌地图SDK,您需要在运行之前将您的应用注册到要嵌入此库的谷歌地图服务,并获取所需的API密钥。
如果您只需要线性线,谷歌地图服务密钥就足够了。但是,如果您想要描绘谷歌方向API的结果中的路线,您还需要提供方向API密钥。
安装
NJCircleLine 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
pod 'NJCircleLine'
使用方法
基本使用
基本使用非常简单。这个库中提供的每个方法都是静态方法,因此您不需要创建实例。只需将所需信息输入到方法中,您就可以使用它了。
// Example for drawing travel line.
// Travel line is the mode that you provide start and end point and the lib will hit Google Direction API and draw the route for you
// Create points
let startPoint = CLLocationCoordinate2D(latitude: 35.452006, longitude: 139.641474)
let endPoint = CLLocationCoordinate2D(latitude: 35.446697, longitude: 139.647305)
// Call the drawTravelLine method, done. (read more if you need further control)
NJCircleLine.drawTravelLine(from: startPoint,
to: endPoint,
on: mapView,
apiKey: "YOUR GOOGLE DIRECTION API KEY",
completion: nil)
绘制线性线条甚至更加简单。
// Example for drawing linear line.
// Linear line is the mode that you provide multiple points and the lib will connect them with dot line
// Create points
let point1 = CLLocationCoordinate2D(latitude: 35.452006, longitude: 139.641474)
let point2 = CLLocationCoordinate2D(latitude: 35.446697, longitude: 139.647305)
// Call the method
NJCircleLine.drawLinearLine(points: [point1, point2],
on: mapView,
completion: nil)
您还可以指定一个视觉配置对象并将其输入到绘制方法中。
// Create a configuration object
var config = NJCircleLineConfiguration()
config.fillColor = UIColor.red
config.strokeWidth = 2.0
config.strokeColor = UIColor.white // Note that it only takes effect when a non-zero strokeWidth is specified
config.minimumInterval = 1.0 // interval between 2 circles
config.circleRadius = 10.0
// Call the method and feed the configuration object
NJCircleLine.drawLinearLine(points: [point1, point2],
on: mapView,
configuration: config,
completion: nil)
高级控制
如果您映射的视图不支持手势,尤其是缩放,那么在大多数情况下,上面的用法应该足够了。但是,如果您正在启用映射视图的缩放功能,则在地图中绘制的圆需要被消除并重新绘制以匹配地图相机。
幸运的是,这样做并不复杂,您只需要持有 NJCircleLine 方法返回的圆的数组和路径的引用。
class NJMapViewController: ViewController {
var circles = [GMSCircle]()
var path: GMSPath? = nil
func draw() {
// Grab the reference in the completion block
NJCircleLine.drawTravelLine(from: startPoint,
to: endPoint,
on: mapView,
apiKey: "YOUR GOOGLE DIRECTION API KEY") { [weak self] (path, circles, _, _, _) in
self?.circles = circles
self?.path = path
}
}
// Once you have the references, you just use them to redraw the line at the desired timing.
// If you wanna achieve what the iOS Google Map app is doing, I recommend doing in the mapViewDidChangePosition delegate method of GMSMapView.
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
// Pass the path and cirles to the low level drawDotLine method.
// Don't forget to get the new reference of circles while calling this method.
circles = NJCircleLine.drawDotLine(path: path, on: mapView, previousCircles: circles)
}
}
就这些!希望您能享受使用!:-)
作者
Jin Nagumo
许可证
NJCircleLine 在 MIT 许可证下提供。有关更多信息,请参阅 LICENSE 文件。