3

I have so many coordinates of a user from server and I want draw line to connect those like this.

I've tried to use This way but my app get crash, I guess this happend because i send too many request.

Wez
  • 10,555
  • 5
  • 49
  • 63
Tung Vu Duc
  • 1,552
  • 1
  • 13
  • 22

2 Answers2

5

The simplest way is to use a GMSPolyline.

Assuming you have a coordinates array of CLLocationCoordinate2D's and they are in the correct order.

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
Wez
  • 10,555
  • 5
  • 49
  • 63
0

- Swift 4 Extensions

Make path with coordinates:

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

Add path to map:

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

Usage:

let path = GMSMutablePath(coordinates: [<#Coordinates#>])
mapView.addPath(path)
Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278