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

时间:2015-07-01 06:11:53

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

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

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:1)

一些建议:

如果您希望在放大和缩小时文本的大小为固定大小,则可以在将文本放入标记图像的位置添加标记。

如果您想在放大和缩小时将文字设置为以米为单位的固定尺寸,则可以添加地面叠加层,将文本放入地面叠加图像中。