为iOS Swift的Google地图中的多边形添加文本标签 [英] Add a text label to a polygon in google maps for iOS swift
本文介绍了为iOS Swift的Google地图中的多边形添加文本标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在iOS中的Google地图的多边形上添加文字标签?我试图添加叠加层,但它只接受图像而不是文本?
这是代码令我困扰的部分:
func updateUIMap(){
var str =Hello
var data = str.dataUsingEncoding(NSUTF8StringEncoding,allowLossyConversion:true)
var drawText = NSString(data:data !,编码:NSUTF8StringEncoding)
let size = CGSize(width:24.0,height:24.0)
var inImage = UIImage()
var textColor:UIColor = UIColor.blackColor()
var textFont:UIFont = UIFont(名称: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()
$ b 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)
让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 !,编码: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 (位置:CLLocationCoordinate2D){
返回newImage
}
func addGroundOverlay(position:CLLocationCoordinate2D){
let overlay = GMSGroundOverlay(position:position, icon:newImage(Hello StackOverflow,大小:CGSizeMake(150.0,150.0)),zoomLevel:10)
覆盖y.bearing = 0
overlay.map =(self.view as! GMSMapView)
}
重写func didReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
//处理任何可以重新创建的资源。
}
}
How do I add a text label on a polygon in Google Maps in iOS ? I tried to add an overlay but it will only accept images not text? I am using google maps for ios with Swift on iOS 9.
This is the portion of code troubling me :
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
}
解决方案
Your UIGraphics
drawing method might not be correct, you can try the following code to make a new image from a text:
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.
}
}
这篇关于为iOS Swift的Google地图中的多边形添加文本标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文