Add text on custom marker on google map for ios

后端 未结 4 1300
青春惊慌失措
青春惊慌失措 2020-12-31 08:56

Am trying to put marker with Textview .Is there any posibility to add text over marker on google map in ios.

  • Like This

4条回答
  •  忘掉有多难
    2020-12-31 09:27

    You must make a view, where you must create an imageView (with your marker image) and Label (with your text) and take a screenshot of that view, and set as icon to your GMSMarker. Something like this:

    - (void)foo
    {
        GMSMarker *marker = [GMSMarker new];
    
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,60,60)];
        UIImageView *pinImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myPin"]];
        UILabel *label = [UILabel new];
        label.text = @"1";
        //label.font = ...;
        [label sizeToFit];
    
        [view addSubview:pinImageView];
        [view addSubview:label];
        //i.e. customize view to get what you need    
    
    
        UIImage *markerIcon = [self imageFromView:view];
        marker.icon = markerIcon;        
        marker.map = self.mapView;      
    }
    
    - (UIImage *)imageFromView:(UIView *) view
    {
        if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
            UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, [[UIScreen mainScreen] scale]);
        } else {
            UIGraphicsBeginImageContext(view.frame.size);
        }
        [view.layer renderInContext: UIGraphicsGetCurrentContext()];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
    

提交回复
热议问题