简体   繁体   English

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

[英]How to label map markers in Google maps ios

I have a few markers on the map and they each represent a path with a different theme. 我在地图上有几个标记,它们各自代表一个具有不同主题的路径。 I want the user to be able to see each of the themes before selecting a marker so I was planning on adding a simple text label above each of them. 我希望用户在选择标记之前能够看到每个主题,因此我计划在每个主题上方添加一个简单的文本标签。 This doesn't seem to be an embedded function in google maps for ios. 这似乎不是谷歌地图为ios的嵌入式功能。 Is there any way around this? 有没有办法解决?

Set up a UILabel , set it up, render it to a UIImage and set that as the marker's icon. 设置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;

I'm a beginner to swift, but I was able to convert Daij-Djan's answer: 我是一个快速的初学者,但我能够转换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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM