0

I am new in iOS development. I am drawing a circle on user location and I want to add the title to the circle. I am using following code

var cirlce: GMSCircle!

let myPlacelatitude = (locationManager.location?.coordinate.latitude)! let myPlacelongitude = (locationManager.location?.coordinate.longitude)!

let userLocation = GMSCameraPosition.camera(withLatitude:  myPlacelatitude,
                                        longitude: myPlacelongitude,
                                        zoom: 7.5)
mapView.camera = userLocation

let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(myPlacelatitude, myPlacelongitude)
marker.title = ""
marker.snippet = ""
marker.map = mapView

let camera = GMSCameraPosition.camera(withLatitude: myPlacelatitude,
                                  longitude: myPlacelongitude, zoom: 6)

cirlce = GMSCircle(position: camera.target, radius: 50000)
cirlce.fillColor = UIColor.green.withAlphaComponent(0.5)
cirlce1.isTappable = true
// the title is not setting to the circle in google map  
cirlce.title = "40 KM"
cirlce.map = mapView
Sahil Manchanda
  • 9,812
  • 4
  • 39
  • 89
omkar marathe
  • 61
  • 1
  • 7
  • There is a similar question, maybe it helps? https://stackoverflow.com/questions/33505355/add-a-text-label-to-a-polygon-in-google-maps-for-ios-swift – KevinP Mar 02 '20 at 07:12

1 Answers1

0

The documentation regarding GMSCircle states that some overlays, such as markers, will display the title on the map. I am not sure if this also holds true when using GMSCircle.

However as a workaround for your issue, you can put label in the GMSCircle when using GMSMarker with UIlabel from the SO answer in the comment of Mamaessen above. Here is the code snippet incorporated in GMSCircle:

class ViewController: UIViewController {

    override func loadView() {
    var circle: GMSCircle!

        let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 9.0)
        let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
        view = mapView
        circle = GMSCircle(position:camera.target , radius: 50000)
        circle.fillColor = UIColor.green.withAlphaComponent(0.5)
        circle.strokeColor = .blue
        circle.strokeWidth = 2
        circle.title = "THIS IS CIRCLE"
        circle.map = mapView

        let labelMarker = GMSMarker(position: camera.target)
        let label = UILabel()
        label.text = circle.title
        label.textColor = UIColor.red
        label.sizeToFit()
        labelMarker.iconView = label
        labelMarker.map = mapView
      }

    }

If you want it to show after you tap the circle just like a popup when the circle is clicked, you can use an image which is a circle and use this as an icon to your marker and your text label should be in the marker title. The following is how I implement it in the code. Just a note you can notice the line marker.icon = UIImage(named: "circle"), this is where you put the name of the image that you want as your marker. In my case I made a New Image Set in my Assets and put the image I want to use as my custom marker.

lass ViewController: UIViewController {

    override func loadView() {
        let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 9.0)
        let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
        view = mapView

        // Creates a marker in the center of the map.
        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
        marker.title = "Sydney"
        marker.icon = UIImage(named: "circle")
        marker.map = mapView
      }
    }

Hope this helps!

Pagemag
  • 2,779
  • 1
  • 7
  • 17