【问题标题】:Add text on custom marker on google map for ios在 ios 的谷歌地图上的自定义标记上添加文本
【发布时间】:2014-06-30 10:10:41
【问题描述】:

我正在尝试使用 Textview 放置标记。是否可以在 ios 的谷歌地图上的标记上添加文本。

  • 像这样

【问题讨论】:

  • 尝试压缩位图并在其上绘制文本。将其保存在缓冲区中并在运行时将其传递给标记图标。

标签: ios objective-c google-maps


【解决方案1】:

您必须创建一个视图,您必须在其中创建一个 imageView(使用您的标记图像)和一个 Label(使用您的文本)并对该视图进行截图,并将其设置为 GMSMarker 的图标。 像这样的:

- (void)foo
{
    GMSMarker *marker = [GMSMarker new];

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,60,60)];
    UIImageView *pinImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myPin"]];
    UILabel *label = [UILabel new];
    label.text = @"1";
    //label.font = ...;
    [label sizeToFit];

    [view addSubview:pinImageView];
    [view addSubview:label];
    //i.e. customize view to get what you need    


    UIImage *markerIcon = [self imageFromView:view];
    marker.icon = markerIcon;        
    marker.map = self.mapView;      
}

- (UIImage *)imageFromView:(UIView *) view
{
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
        UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, [[UIScreen mainScreen] scale]);
    } else {
        UIGraphicsBeginImageContext(view.frame.size);
    }
    [view.layer renderInContext: UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

【讨论】:

  • 您好,我按照相同的程序进行操作,但问题是我的图像没有显示。
【解决方案2】:

如果您想显示类似的内容,请按照以下步骤操作。很简单,你可以用这个方法。

-(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;}

当您在地图上设置标记时,只需设置

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

【讨论】:

    【解决方案3】:

    这是Kunal's answerSwift 5版本:

    //count is the integer that has to be shown on the marker
    func createImage(_ count: Int) -> UIImage {
    
        let color = UIColor.red
        // select needed color
        let string = "\(UInt(count))"
        // the string to colorize
        let attrs: [NSAttributedString.Key: Any] = [.foregroundColor: color]
        let attrStr = NSAttributedString(string: string, attributes: attrs)
        // add Font according to your need
        let image = UIImage(named: "ic_marker_orange")!
        // The image on which text has to be added
        UIGraphicsBeginImageContext(image.size)
        image.draw(in: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(image.size.width), height: CGFloat(image.size.height)))
        let rect = CGRect(x: CGFloat(20), y: CGFloat(5), width: CGFloat(image.size.width), height: CGFloat(image.size.height))
    
        attrStr.draw(in: rect)
    
        let markerImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return markerImage
    }  
    

    希望这对其他人有所帮助。

    【讨论】:

    【解决方案4】:

    func createImage(_ count: Int) -> UIImage? {
        let string = String(count)
        let paragraphStyle = NSMutableParagraphStyle()
        let font = UIFont.systemFont(ofSize: 9.5)
        paragraphStyle.alignment = .center
        let attributes: [NSAttributedString.Key: Any] = [
            .font : font,
            .foregroundColor: UIColor.black,
            .paragraphStyle: paragraphStyle
        ]
        
        let attrStr = NSAttributedString(string: string, attributes: attributes)
    
        let image = self.imageWithImage(image: UIImage(named: "pin_online.png")!, scaledToSize: CGSize(width: 50.0, height: 60.0))
        UIGraphicsBeginImageContext(image.size)
        UIGraphicsBeginImageContextWithOptions(image.size, false, 0.0)
        image.draw(in: CGRect(x: 0, y: 0, width: CGFloat(image.size.width), height: CGFloat(image.size.height)))
        let rect = CGRect(x: 14.7, y: CGFloat(3), width: CGFloat(image.size.width), height: CGFloat(image.size.height))
        attrStr.draw(in: rect)
        let markerImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    
        return markerImage
    }
    

    【讨论】:

      猜你喜欢
      • 2018-11-15
      • 1970-01-01
      • 2016-03-16
      • 1970-01-01
      • 1970-01-01
      • 2019-12-01
      • 1970-01-01
      相关资源
      最近更新 更多