gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 08:20:46 24 4
gpt4 key购买 nike

如何在 iOS 中的 Google map 的多边形上添加文本标签?我试图添加一个叠加层,但它只接受图像而不接受文本?我在 iOS 9 上使用 google maps for 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, 0, size.width, size.height))

var rect: CGRect = CGRectMake(24, 24, size.width, 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, 0, size.width, 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.
}

}

enter image description here

关于ios - 在 iOS swift 的谷歌地图中为多边形添加文本标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33505355/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com