我想给 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/