Add text on custom marker on google map for ios

后端 未结 4 1283
青春惊慌失措
青春惊慌失措 2020-12-31 08:56

Am trying to put marker with Textview .Is there any posibility to add text over marker on google map in ios.

  • Like This

4条回答
  •  不思量自难忘°
    2020-12-31 09:41

    Here is the Swift 5 version of Kunal's answer:

    //count is the integer that has to be shown on the marker
    func createImage(_ count: Int) -> UIImage {
    
        let color = UIColor.red
        // select needed color
        let string = "\(UInt(count))"
        // the string to colorize
        let attrs: [NSAttributedString.Key: Any] = [.foregroundColor: color]
        let attrStr = NSAttributedString(string: string, attributes: attrs)
        // add Font according to your need
        let image = UIImage(named: "ic_marker_orange")!
        // The image on which text has to be added
        UIGraphicsBeginImageContext(image.size)
        image.draw(in: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(image.size.width), height: CGFloat(image.size.height)))
        let rect = CGRect(x: CGFloat(20), y: CGFloat(5), width: CGFloat(image.size.width), height: CGFloat(image.size.height))
    
        attrStr.draw(in: rect)
    
        let markerImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return markerImage
    }  
    

    Hope this helps someone else.

提交回复
热议问题