如何在Google maps ios中标记地图标记

如何在Google maps ios中标记地图标记,ios,objective-c,google-maps,google-maps-markers,google-maps-sdk-ios,Ios,Objective C,Google Maps,Google Maps Markers,Google Maps Sdk Ios,我在地图上有几个标记,每个标记代表一条具有不同主题的路径。我希望用户在选择标记之前能够看到每个主题,因此我计划在每个主题上方添加一个简单的文本标签。这似乎不是谷歌地图ios版中的嵌入式功能。有什么办法可以解决这个问题吗?设置一个UILabel,设置它,将它渲染为UIImage,并将其设置为标记的图标 //setup label UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 20)]; label.text

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

设置一个
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;

我是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()

如果你想要一个标签和一个别针,把它们都渲染成一个图像,你可以捕捉任何你喜欢的视图。谢谢你的完整答案,现在就试试吧。我想到了这一点,但我不知道renderInContext是一个选项,只是尝试了一下,效果很好,尽管我添加了[label sizeToFit];就在您将文本设置为“test”的行之后,这非常棒!谢谢唯一不好的是,使用大量标记(例如300个标记)时速度会变慢。。它们真的各不相同吗?同时在地图上呢?我将其与缓存结合使用,可以处理1000个标记