向 GMSMapView 添加文本 - 在 Google 地图上写入 iOS

Add text to GMSMapView - write on Google Map for iOS

我想向 GMSMapView 添加一些文本标签。当然,文本应该随着地图的放大和缩小而调整大小。我在 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;
}