添加文本到GMSMapView - 在Google Map for iOS上写

eta*_*luz 4 google-maps ios google-maps-sdk-ios

我想在GMSMapView中添加一些文本标签.当然,当地图放大和缩小时,文本应该调整大小.我没有在Google Map SDK中看到文本对象.我该怎么做呢?

eta*_*luz 5

这是我从@ 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;
}
Run Code Online (Sandbox Code Playgroud)