ios - 在 iOS 版谷歌地图上的自定义标记上添加文本

标签 ios objective-c google-maps

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

  • 像这样

Like this

最佳答案

您必须制作一个 View ,您必须在其中创建一个 imageView(带有您的标记图像)和 Label(带有您的文本)并截取该 View 的屏幕截图,并将其设置为 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;
}

关于ios - 在 iOS 版谷歌地图上的自定义标记上添加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24487717/

相关文章:

ios - coredata NSFetchRequest 一对多关系 托管对象上的关系错误

objective-c - 如何从选定的 Twitter 帐户发送推文

iOS - 将 NSDate 与另一个 NSDate 进行比较

ios - UIView 背景颜色设置不是显示的内容

android - 读取 LatLngs 的 csv 文件,添加到 ArrayList,然后打印到谷歌地图 fragment

ios - 未计算 UITableViewCell 中的约束

ios - 以编程方式向 UINavigationBar 添加提示

ios - 自定义 Action Controller ,Swift

android - Android 中 mapview 上的地理定位图像

javascript - 使用坐标在 Google map 中创建边界区域