Add text on custom marker on google map for ios

后端 未结 4 1279
青春惊慌失措
青春惊慌失措 2020-12-31 08:56

Am trying to put marker with Textview .Is there any posibility to add text over marker on google map in ios.

  • Like This

4条回答
  •  失恋的感觉
    2020-12-31 09:41

    If you want to display something like this , then just follow these steps. It is very simple, You can use this method.

    -(UIImage *)createImage:(NSUInteger)count{   //count is the integer that has to be shown on the marker 
    
    
    UIColor *color = [UIColor redColor]; // select needed color
    NSString *string = [NSString stringWithFormat:@"%lu",(unsigned long)count]; // the string to colorize
    NSDictionary *attrs = @{ NSForegroundColorAttributeName : color };
    NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:string attributes:attrs]; // add Font according to your need
    
    UIImage *image = [UIImage imageNamed:@"ic_marker_orange"]; // The image on which text has to be added
    UIGraphicsBeginImageContext(image.size);
    [image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
    
    CGRect rect = CGRectMake(20,5, image.size.width, image.size.height);// change the frame of your text from here
    [[UIColor whiteColor] set];
    [attrStr drawInRect:rect];
    UIImage *markerImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return markerImage;}
    

    and when you set marker to the map then just set

        GMSMarker *marker = [[GMSMarker alloc] init];
        marker.icon = [self createImage:[model.strFriendCount integerValue]]; // pass any integer to the method.
    

提交回复
热议问题