Yash Bedi
Yash Bedi

Reputation: 1355

Draw Dashed Circle on Google Maps : iOS

I've been trying so hard to Draw a Dashed Circle on Google Maps but couldn't find anything helping...

I've been looking over the internet literally for days to find some solutions for drawing a Dashed circle on GoogleMaps, unfortunately other than drawing a plain circle is the answer what I get every time..

Here's what I did :

map with circle

Code for the above is :

import UIKit
import GoogleMaps
import GooglePlaces

class ViewController: UIViewController
{
    @IBOutlet weak var gmsMapView: GMSMapView!

    override func viewDidLoad()
    {
        super.viewDidLoad()

        gmsMapView.isMyLocationEnabled = true
        gmsMapView.settings.myLocationButton = true
        gmsMapView.animate(toZoom: 10.0)
        gmsMapView.animate(toLocation: CLLocationCoordinate2D(latitude: 40.709677, longitude: -74.011088))
        let circleCenter = CLLocationCoordinate2D(latitude: 40.709677, longitude: -74.011088)
        let circle = GMSCircle(position: circleCenter, radius: 5000)
        circle.strokeWidth = 2
        circle.strokeColor = UIColor.blue
        circle.map = gmsMapView
    }

    override func didReceiveMemoryWarning(){
        super.didReceiveMemoryWarning()
    }
}

This is what is required :

enter image description here

Upvotes: 3

Views: 1656

Answers (1)

stfnhdr
stfnhdr

Reputation: 106

With GMSCircle its not possible, you have to draw a polyline based on a circle.

With GMSGeometryOffset you can generate an offset to your location. 360/6 = 60 for less detail an efficiency.

let path = GMSMutablePath()

for i in 0...60 {
    let offsetLocation = GMSGeometryOffset(location.coordinate, circleRadius, CLLocationDirection(i*6))
    path.add(offsetLocation)
}
let length: [NSNumber] = [10, 10]
let circle = GMSPolyline(path: path)
let styles = [GMSStrokeStyle.solidColor(.clear), GMSStrokeStyle.solidColor(.red)]

circle.spans = GMSStyleSpans(circle.path!, styles, length, GMSLengthKind.rhumb)
circle.strokeWidth = 1.0
circle.map = mapView

Upvotes: 9

Related Questions