【问题标题】:Swift 3 draw a polyline with Google Maps during runSwift 3 在运行期间使用 Google Maps 绘制折线
【发布时间】:2017-05-12 07:32:27
【问题描述】:

我在下面找到了使用 Apple 地图在跑步/步行期间绘制路径的方法

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
  }
}

但是,我尝试使用 Google 地图执行此操作,但找不到解决方案。 苹果地图的答案很多,但谷歌地图的答案不多。

【问题讨论】:

  • 是否要使用用户位置绘制折线?
  • @RajeshkumarR 是的,在他移动时画一条折线
  • 在locationManager的委托方法didUpdateLocations这样使用path.add(locations.last.coordinate)

标签: ios swift google-maps polyline google-polyline


【解决方案1】:

试试这个

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

【讨论】:

    【解决方案2】:

    这是我使用 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
                    
                }
                
            }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-10-28
      • 1970-01-01
      • 1970-01-01
      • 2018-09-01
      • 2015-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多