Google地图:
我是创建了一个单例来实现google相关的东西
这个是画线的方法
这里只是画出一条线 并不是虚线
/// 画线
func addGoogleLine(mapView: T,mapLineModel:[CLLocationCoordinate2D]) -> GMSPolyline{
if mapLineModel.count <= 1{return GMSPolyline()}
let polyline = GMSPolyline()
linepath.removeAllCoordinates()
polyline.strokeWidth = 4
mapLineModel.forEach { (item) in
// polyline.strokeColor = UIColor.xFF3366
linepath.add(item)
}
polyline.path = linepath
polyline.geodesic = true
polyline.map = mapView
mapView.animate(with: GMSCameraUpdate.fit(GMSCoordinateBounds(coordinate: mapLineModel.first!, coordinate: mapLineModel.last!), with: UIEdgeInsets(top: 64, left: 30, bottom: 30, right: 30)))
return polyline
}
虚线需要在GMSMapViewDelegate下面这个回调中实现
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
updateLine()
}
///设置虚线
private func updateLine() {
if self.googleMapView != nil {
let styles: [GMSStrokeStyle] = [.solidColor(UIColor.xFF3366), .solidColor(.clear)]
let scale = 1.0 / self.googleMapView!.projection.points(forMeters: 1, at: self.googleMapView!.camera.target)
let solidLine = NSNumber(value: 15.0 * Float(scale))
let gap = NSNumber(value: 10.0 * Float(scale))
if self.polyline.path != nil {
self.polyline.spans = GMSStyleSpans(self.polyline.path!, styles, [solidLine, gap], GMSLengthKind.rhumb)
}
}
}
高德地图:
需要在 MAMapViewDelegate
设置线的样式
func mapView(_ mapView: MAMapView!, rendererFor overlay: MAOverlay!) -> MAOverlayRenderer! {
///画线的样式
if overlay.isKind(of: MAPolyline.self) {
let model = MAPolylineRenderer(overlay: overlay)
model?.lineWidth = 5
model?.strokeColor = UIColor.xFF3366
model?.fillColor = UIColor.x000000
model?.lineJoinType = kMALineJoinRound
model?.lineCapType = kMALineCapArrow
model?.lineDashType = .square
return model
}
return nil
}
///自己的单例方法 将多个坐标折现添加到地图上 使地图展示线段大小
func addMoreLine(mapView: MAMapView,lineCoordinates: [CLLocationCoordinate2D]) {
var coordinates = lineCoordinates
if coordinates.count <= 0{ return }
if let oldMAPolyline = oldMAPolyline {
mapView.remove(oldMAPolyline)
}
let polyline = MAPolyline(coordinates: &coordinates, count: UInt(lineCoordinates.count))
mapView.add(polyline)
let rect = mapView.mapRectThatFits(polyline?.boundingMapRect ?? MAMapRect(origin: MAMapPoint(x: lineCoordinates.first?.longitude ?? 0, y: lineCoordinates.first?.latitude ?? 0), size: MAMapSize(width: Double(ScreenInfo.Width), height: Double(ScreenInfo.Height - 193))))
mapView.setVisibleMapRect(rect, edgePadding: UIEdgeInsets(top: 40,left: 40,bottom: 40,right: 40), animated: true)
oldMAPolyline = polyline
}
原文:https://blog.csdn.net/weiwandaixu_/article/details/86673013