Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Place Name Label to Google Map Markers in iOS

I have markers displayed on a Google Map and would like to add a UILabel of the name of each respective place.

Saw that the GMSMarker property title is specific to a marker's infoWindow and from the slim pickings of the available research, a custom icon might have to be made?

Are there any iOS examples of (easier) implementation?

like image 424
Chris Avatar asked Oct 16 '25 13:10

Chris


1 Answers

In order to create a custom marker you have to subclass GMSMarker and create it's iconView according to your needs.

From Google Maps documentation:

  • (UIView*) iconView [read, write, assign]

Marker view to render. If left nil, falls back to the icon property instead.

Example:

class CustomMarker: GMSMarker {

    var label: UILabel!

    init(labelText: String) {
        super.init()

        let iconView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 50, height:     80)))
        iconView.backgroundColor = .white

        label = UILabel(frame: CGRect(origin: .zero, size: CGSize(width:     iconView.bounds.width, height: 40)))
        label.text = labelText
        iconView.addSubview(label)

        self.iconView = iconView
    }
}

Usage:

let marker = CustomMarker(labelText: "my_label")
marker.position = CLLocationCoordinate2D(latitude: 0, longitude: 0)
marker.map = //your mapView object

Note that iconView is only used to create a markdown for your marker to be rendered. Your marker won't be intractable as a usual UIView, so adding any gestures or complex scrollable views to it won't take any effect.

like image 97
Vadim Popov Avatar answered Oct 18 '25 03:10

Vadim Popov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!