HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了为iOS swift的谷歌地图中的多边形添加文本标签大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
如何在iOS中的Google地图中的多边形上添加文本标签我试添加叠加但它只接受图像而不是文字?我在iOS 9上使用谷歌地图为ios与 Swift.

这是困扰我的代码部分:

func updateUIMap(){

            var str = "Hello"
            var data = str.dataUsingEncoding(NSUTF8StringEncoding,allowLossyConversion: truE)
            var drawText = NSString(data: data!,encoding: NSUTF8StringEncoding)
            let size = CGSize(width: 24.0,height: 24.0)

           var inImage = UIImage()
            var textColor: UIColor = UIColor.blackColor()
            var textFont: UIFont = UIFont(name: "Helvetica Bold",size: 12)!
            UIGraphicsBeginImageContext(sizE)
            let textFontAttributes = [
                NSFontAttributename: textFont,NSForegroundColorAttributename: textColor,]

            inImage.drawInRect(CGRectMake(0,size.width,size.height))

            var rect: CGRect = CGRectMake(24,24,size.height)

            drawText.drawInRect(rect,withAttributes: textFontAttributes)

            var newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()


            UIGraphicsEndImageContext()

            var overlay = GMSGroundOverlay(position: myposition,icon: newImage,zoomLevel:20)
            overlay.bearing = 0
            overlay.map = self.mapView
}

解决方法

您的UIGraphics绘图方法可能不正确,您可以尝试以下代码从文本中创建新图像:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        super.viewDidLoad()

        let camera = GMSCameraPosition.cameraWithLatitude(40.712216,longitude: -74.22655,zoom: 10)
        let mapView = GMsmapView.mapWithFrame(CGRectZero,camera: camera)
        mapView.myLOCATIOnEnabled = true
        self.view = mapView

        addGroundOverlay(camera.target)
    }

    func newImage(text: String,size: CGSizE) -> UIImage {

        let data = text.dataUsingEncoding(NSUTF8StringEncoding,allowLossyConversion: truE)
        let drawText = NSString(data: data!,encoding: NSUTF8StringEncoding)

        let textFontAttributes = [
            NSFontAttributename: UIFont(name: "Helvetica Bold",size: 20)!,NSForegroundColorAttributename: UIColor.redColor(),]

        UIGraphicsBeginImageContextWithOptions(size,false,0)
        drawText?.drawInRect(CGRectMake(0,size.height),withAttributes: textFontAttributes)
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return newImage
    }

    func addGroundOverlay(position: CLLOCATIOnCoordinate2D) {

        let overlay = GMSGroundOverlay(position: position,icon: newImage("Hello StackOverflow",size: CGSizeMake(150.0,150.0)),zoomLevel: 10)
        overlay.bearing = 0
        overlay.map = (self.view as! GMsmapView)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

为iOS swift的谷歌地图中的多边形添加文本标签

大佬总结

以上是大佬教程为你收集整理的为iOS swift的谷歌地图中的多边形添加文本标签全部内容,希望文章能够帮你解决为iOS swift的谷歌地图中的多边形添加文本标签所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。