Search code examples
iosobjective-cgoogle-mapsgoogle-maps-markersgoogle-maps-sdk-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. Is there any way around this?


Solution

  • Set up a UILabel, set it up, render it to a UIImage and set that as the marker's icon.

    //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;