【问题标题】:How to draw line on google maps如何在谷歌地图上画线
【发布时间】:2019-02-18 16:11:40
【问题描述】:

我有很多来自服务器的用户坐标,我想画线来连接像this 这样的坐标。

我尝试使用This way,但我的应用程序崩溃了,我猜这是因为我发送了太多请求。

【问题讨论】:

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


    【解决方案1】:

    最简单的方法是使用GMSPolyline

    假设您有一个由CLLocationCoordinate2D 组成的coordinates 数组,并且它们的顺序正确。

    let path = GMSMutablePath()
    for coord in coordinates {
        path.add(coord)
    }
    let line = GMSPolyline(path: path)
    line.strokeColor = UIColor.blue
    line.strokeWidth = 3.0
    line.map = self.map
    

    【讨论】:

      【解决方案2】:

      - Swift 4 扩展

      用坐标制作路径:

      extension GMSMutablePath {
          convenience init(coordinates: [CLLocationCoordinate2D]) {
              self.init()
              for coordinate in coordinates {
                  add(coordinate)
              }
          }
      }
      

      添加地图路径:

      extension GMSMapView {
          func addPath(_ path: GMSPath, strokeColor: UIColor? = nil, strokeWidth: CGFloat? = nil, geodesic: Bool? = nil, spans: [GMSStyleSpan]? = nil) {
              let line = GMSPolyline(path: path)
              line.strokeColor = strokeColor ?? line.strokeColor
              line.strokeWidth = strokeWidth ?? line.strokeWidth
              line.geodesic = geodesic ?? line.geodesic
              line.spans = spans ?? line.spans
              line.map = self
          }
      }
      

      用法:

      let path = GMSMutablePath(coordinates: [<#Coordinates#>])
      mapView.addPath(path)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-12
        • 2017-12-06
        • 2011-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多