5

我在地图上有几个标记,它们每个都代表具有不同主题的路径。我希望用户能够在选择标记之前看到每个主题,因此我计划在每个主题上方添加一个简单的文本标签。这似乎不是 ios 谷歌地图中的嵌入式功能。有没有办法解决?

4

2 回答 2

23

设置 a UILabel,设置它,将其渲染为 aUIImage并将其设置为标记的图标。

//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;
于 2013-04-04T19:47:57.377 回答
2

我是 swift 的初学者,但我能够转换 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()
于 2016-03-04T22:50:48.367 回答