我想在GMSMapView中添加一些文本标签。当然,随着地图缩放,文本应该自动调整大小。在Google Map SDK中,我没有看到文本对象。那么我该如何实现呢?
我想在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;
}