我在地图上有几个标记,它们各自代表一个具有不同主题的路径。我希望用户在选择标记之前能够看到每个主题,因此我计划在每个主题上方添加一个简单的文本标签。这似乎不是谷歌地图为ios的嵌入式功能。有没有办法解决这个问题?
答案 0 :(得分:22)
设置UILabel
,对其进行设置,将其渲染为UIImage
并将其设置为标记的图标。
//setup label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
label.text = @"test";
label.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.5];
//grab it
UIGraphicsBeginImageContextWithOptions(label.bounds.size, NO, [[UIScreen mainScreen] scale]);
[label.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * icon = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//prepare options
GMSMarkerOptions *options = [GMSMarkerOptions options];
options.position = CLLocationCoordinate2DMake(latitude, longitude);
options.title = [NSString stringWithFormat:@"#%d", count_];
options.icon = icon;
答案 1 :(得分:2)
我是一个快速的初学者,但我能够转换Daij-Djan的答案:
let label = UILabel()
label.frame = CGRect(x:0, y:0, width: 50, height: 20)
label.text = "test"
label.backgroundColor = UIColor.redColor().colorWithAlphaComponent(0.5)
label.textColor = UIColor.whiteColor()
label.adjustsFontSizeToFitWidth = true
UIGraphicsBeginImageContextWithOptions(label.frame.size, false, UIScreen.mainScreen().scale)
if let currentContext = UIGraphicsGetCurrentContext()
{
label.layer.renderInContext(currentContext)
let imageMarker = UIImage()
imageMarker = UIGraphicsGetImageFromCurrentImageContext()
let marker = GMSMarker(position: CLLocationCoordinate2DMake(lat, long))
marker.icon = imageMarker
marker.map = mapView
}
UIGraphicsEndImageContext()