Ios Swift 3在运行期间使用谷歌地图绘制多段线
我找到了以下使用Apple Maps在跑步/步行时绘制路径的方法Ios Swift 3在运行期间使用谷歌地图绘制多段线,ios,swift,google-maps,polyline,google-polyline,Ios,Swift,Google Maps,Polyline,Google Polyline,我找到了以下使用Apple Maps在跑步/步行时绘制路径的方法 extension NewRunViewController: MKMapViewDelegate { func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! { if !overlay.isKindOfClass(MKPolyline) { return nil
extension NewRunViewController: MKMapViewDelegate {
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
if !overlay.isKindOfClass(MKPolyline) {
return nil
}
let polyline = overlay as! MKPolyline
let renderer = MKPolylineRenderer(polyline: polyline)
renderer.strokeColor = UIColor.blueColor()
renderer.lineWidth = 3
return renderer
}
}
然而,我正试图用谷歌地图做这件事,却找不到解决方案。
很多答案都是针对苹果地图的,但谷歌地图上的答案并不多。试试这个
let path = GMSMutablePath()
//Change coordinates
path.add(CLLocationCoordinate2D(latitude: -33.85, longitude: 151.20))
path.add(CLLocationCoordinate2D(latitude: -33.70, longitude: 151.40))
path.add(CLLocationCoordinate2D(latitude: -33.73, longitude: 151.41))
let polyline = GMSPolyline(path: path)
polyline.strokeColor = UIColor.blue
polyline.strokeWidth = 3.0
polyline.map = mapView
这是我对Swift 5的解决方案
private var trackLocations: [CLLocation] = []
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
for location in locations {
self.trackLocations.append(location)
let index = self.trackLocations.count
if index > 2 {
let path = GMSMutablePath()
path.add(self.trackLocations[index - 1].coordinate)
path.add(self.trackLocations[index - 2].coordinate)
let polyline = GMSPolyline(path: path)
polyline.strokeColor = Global.iOSMapRendererColor
polyline.strokeWidth = 5.0
polyline.map = self.mapView
}
}
}
是否要使用用户位置绘制多段线?@RajeshkumarR是的,在locationManager的委托方法中绘制移动的多段线
didUpdateLocations
如下使用path.add(locations.last.coordinate)