Ios 在不调整大小的情况下向GMSMapView添加文本
我需要添加一些文本字符串到我的GMSMapView,可以像解决前面的问题一样在这里(从UILabel获取UIImage,然后将该图像作为图标添加到GMSMapView的GMSGroundOverlay),但我需要添加文本,当用户更改地图缩放时,该文本不会调整大小Ios 在不调整大小的情况下向GMSMapView添加文本,ios,swift,google-maps-sdk-ios,Ios,Swift,Google Maps Sdk Ios,我需要添加一些文本字符串到我的GMSMapView,可以像解决前面的问题一样在这里(从UILabel获取UIImage,然后将该图像作为图标添加到GMSMapView的GMSGroundOverlay),但我需要添加文本,当用户更改地图缩放时,该文本不会调整大小 你们可以看到,当用户改变地图的缩放比例时,我的标签也会改变字体大小,但我需要添加标签,它看起来和街道名称一样——缩放时不需要调整大小。如何制作?根据您的要求,为什么不使用GMSMarker而不是GMSGroundOverlay 这个
你们可以看到,当用户改变地图的缩放比例时,我的标签也会改变字体大小,但我需要添加标签,它看起来和街道名称一样——缩放时不需要调整大小。如何制作?根据您的要求,为什么不使用
GMSMarker
而不是GMSGroundOverlay
这个代码对我有用
- (UIImage *) imageWithView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
添加标签标记
UILabel *label = [UILabel new];
label.text = @"Job";
label.opaque = NO;
label.font = [UIFont fontWithName: @"Arial" size: 14];
label.textColor = [UIColor blackColor];
label.backgroundColor = [UIColor whiteColor];
label.textAlignment = NSTextAlignmentCenter;
label.layer.borderWidth = 2.0;
label.layer.borderColor = [UIColor blackColor].CGColor;
label.layer.masksToBounds = true;
label.layer.cornerRadius = 50;
label.frame = CGRectMake(0, 0, 100, 100);
GMSMarker *title = [GMSMarker markerWithPosition:centerPosition];
title.icon = [self imageWithView:label];
title.map = mapView;
基于@BùiĐứ上面是c Khánh的答案,Swift4
import UIKit
import CoreLocation
import GoogleMaps
extension GMSMarker {
convenience init(position: CLLocationCoordinate2D, text: String, backgroundColor: UIColor? = .white) {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
label.isOpaque = false
label.font = UIFont(name: "Arial", size: 14)
label.textColor = .white
label.backgroundColor = backgroundColor ?? .white
label.textAlignment = .center
//label.layer.borderWidth = 2.0
//label.layer.borderColor = UIColor.black.cgColor
//label.layer.masksToBounds = true
//label.layer.cornerRadius = 50
label.text = text
self.init(position: position)
icon = UIImage.imageFromView(label)
}
}
extension UIImage {
class func imageFromView(_ view: UIView) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.isOpaque, 0.0)
guard let context = UIGraphicsGetCurrentContext() else { return nil }
view.layer.render(in: context)
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return img
}
}
用法:
let marker = GMSMarker(position: CLLocationCoordinate2DMake(40.1, 25.5),
text: "Lorem ipsum",
backgroundColor: .green)
marker.map = self.mapView
您好,找到解决方案了吗?没有,没有找到解决方案:(您是如何将“作业”文本设置为GMSGroundOverlay中心的?我已将标签转换为图像并设置为GMSGroundOverlay图标。但它不会设置在GMSGroundOverlay的中心。您好。问题标记为“Swift”,不是Objective-C。请发布Swift版本,以便OP可以使用您的解决方案。谢谢。