Tung Vu Duc
Tung Vu Duc

Reputation: 1662

How to draw line on google maps

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.

Upvotes: 3

Views: 2223

Answers (2)

Mojtaba Hosseini
Mojtaba Hosseini

Reputation: 119686

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

Upvotes: 0

Wez
Wez

Reputation: 10712

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

Upvotes: 6

Related Questions