ios - 向 GMSMapView 添加文本 - 在 Google Map for iOS 上书写

标签 ios google-maps google-maps-sdk-ios

我想给 GMSMapView 添加一些文本标签。当然,文本应该随着 map 的放大和缩小而调整大小。我在 Google Map SDK 中没有看到文本对象。我该怎么做?

最佳答案

这是我根据@Saxon 的建议提出的解决方案,它是从我的项目中复制和粘贴的,因此您必须进行修改以满足您的需要。这个想法是将 UILabel 呈现为 UIImage,然后使用该图像设置 GMSGroundOverlay 图标。然后它调整大小和所有。 GMSCoordinateBounds 是用 NorthEast 和 SouthWest 点构建的,所以如果你想将标签居中于 CenterX 和 CenterY 点,你需要按如下方式计算它。

  UILabel *label = [UILabel new];      
  label.opaque = NO;
  label.text = @"H";
  label.font = [UIFont fontWithName: @"Arial-BoldMT" size: Normalize(32)];
  label.textColor = [UIColor blueColor];
  label.frame = CGRectMake(0, 0, Normalize(140), Normalize(140));
  label.textAlignment = NSTextAlignmentCenter;

  CLLocationDegrees offset = Normalize(0.03);  // change size here
  CLLocationDegrees southWestY = centerY - offset;
  CLLocationDegrees southWestX = centerX - offset;
  CLLocationDegrees northEastY = centerY + offset;
  CLLocationDegrees northEastX = centerX + offset;

  CLLocationCoordinate2D southWest = CLLocationCoordinate2DMake(southWestY,southWestX);
  CLLocationCoordinate2D northEast = CLLocationCoordinate2DMake(northEastY,northEastX);
  GMSCoordinateBounds *overlayBounds = [[GMSCoordinateBounds alloc] initWithCoordinate:southWest coordinate:northEast];

  GMSGroundOverlay *overlay = [GMSGroundOverlay groundOverlayWithBounds:overlayBounds icon:[self imageWithView:label]];
  overlay.bearing = 0;
  overlay.map = self.mapView

- (UIImage *) imageWithView:(UIView *)view
{
  UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
  [view.layer renderInContext:UIGraphicsGetCurrentContext()];
  UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  return img;
}

关于ios - 向 GMSMapView 添加文本 - 在 Google Map for iOS 上书写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31153752/

相关文章:

iphone - ios 动画 Objective-C

iOS - 我可以将 xib 文件用于动态内容和固定内容混合的可变高度表格单元吗

ios - 如何删除位于可见区域之外的图 block 图层 Google map SDK for iOS

javascript - 带有 jquery 的 Google 地理 map 在 IE 中失败

ios - GoogleMaps for iOS infoWindow 与来自实体的数据进行搜索

iphone - 适用于 iOS 的 Google map APi : UIActivityView on custom info window

ios - 如何将 iOS 键盘背景更改为更深的颜色,即黑色?

iphone - 创建 CorePlot 图

php - 谷歌地图标记加载最后一条记录

javascript - 使用 MVC 在 googlemap 上创建多边形并将结果保存到数据库