iosswiftgoogle-mapsgoogle-maps-markersgoogle-maps-sdk-ios

google maps iOS SDK: custom icons to be used as markers


The Android API has a very convenient class for this, IconGenerator. Using the IconGenerator in my Android app, I can easily make a marker that:

  1. is a simple rectangle with the color of my choosing.
  2. resizes to hold text of any length.
  3. is NOT an info window - I'd like the marker itself to contain the text as shown in the image below from the android version.

enter image description here

// Android - problem solved with IconGenerator
IconGenerator iconGenerator = new IconGenerator(context);
iconGenerator.setStyle(IconGenerator.STYLE_GREEN); // or any other color
Bitmap iconBitmap = iconGenerator.makeIcon(myString);
Marker m = new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(iconBitmap))
                              .position(myLatLng);
map.addMarker(m); // map is a com.google.android.gms.maps.GoogleMap

Is there a way to do something as simple as this in iOS using Swift? There has been a recent release of the iOS api that allows "marker customization", but I don't see how to apply it to this use case.

// iOS (Swift) - I don't know how to create the icon as in code above
let marker = GMSMarker(position: myLatLng)
marker.icon = // How can I set to a rectangle with color/text of my choosing?
marker.map = map // map is a GMSMapView

Solution

  • Here is what I have done

    let marker = GMSMarker()
    
    // I have taken a pin image which is a custom image
    let markerImage = UIImage(named: "mapMarker")!.withRenderingMode(.alwaysTemplate)
    
    //creating a marker view
    let markerView = UIImageView(image: markerImage)
    
    //changing the tint color of the image
    markerView.tintColor = UIColor.red
    
    marker.position = CLLocationCoordinate2D(latitude: 28.7041, longitude: 77.1025)
    
    marker.iconView = markerView
    marker.title = "New Delhi"
    marker.snippet = "India"
    marker.map = mapView
    
    //comment this line if you don't wish to put a callout bubble
    mapView.selectedMarker = marker
    

    The output is

    enter image description here

    And my marker image was

    enter image description here

    You can change your color as per your need. Also if you want something in rectange, you can just create a simple small rectangular image and use it like I did above and change the color of your need.

    Or if you want a rectangle with text within it, you can just create a small UIView with some label and then convert that UIView in UIImage and can do the same thing.

    //function to convert the given UIView into a UIImage
    func imageWithView(view:UIView) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0.0)
        view.layer.render(in: UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!
    }
    

    Hope it helps!!