如何在Google地图上标注地图标记ios

问题描述:

我在地图上有几个标记,它们分别代表一个具有不同主题的路径。我希望用户在选择标记之前能够看到每个主题,所以我打算在每个主题上添加一个简单的文本标签。这似乎不是ios的谷歌地图中的嵌入式功能。有没有办法解决?如何在Google地图上标注地图标记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; 
+0

未来 – 2013-04-04 19:52:29

+0

如果你想有一个标签和销示例代码,使其既成为一个图像,你可以捕捉任何你喜欢的视图 – 2013-04-04 19:59:47

+0

哇感谢您的完整答案,现在就试试。我想到了这一点,但我不知道renderInContext是一个选项 – CSStudent 2013-04-05 00:02:52

我是一个初学者迅速,但我可以转换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()