简体   繁体   中英

Is it possible to display multiple info windows for multiple markers without tapping it?

I want to display multiple info window for multiple markers in google map. The info window should display without tapping the marker itself. Is it possible? After researching, I learned that setting the marker as mapview selected marker can make the info window appear without tapping it. However, multiple markers cannot be selected as the selected marker of the mapview at a time. Is there anything that can be done?

Here is the code to create custom markers as shown in the above image:

Create a subclass of UIView and add the below method to the class.

-(UIImage*)createCustomMarkerImageWithMarker:(GMSMarker *)marker
{
    CGRect priceLabelRect = [marker.title boundingRectWithSize:CGSizeMake(500, 50)
                                                       options:NSStringDrawingUsesLineFragmentOrigin
                                                    attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10]}
                                                       context:nil];

    UILabel *priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, priceLabelRect.size.width+25, priceLabelRect.size.height+12)];
    priceLabel.text = [NSString stringWithFormat:@" ₹ %@ ",marker.title];
    priceLabel.textAlignment = NSTextAlignmentCenter;
    priceLabel.textColor = [UIColor blackColor];
    priceLabel.backgroundColor = [UIColor clearColor];
    priceLabel.font = [UIFont systemFontOfSize:11];



    CGRect numberOfPropertiesLabelRect = [marker.snippet boundingRectWithSize:CGSizeMake(300, 50)
                                                                      options:NSStringDrawingUsesLineFragmentOrigin
                                                                   attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10]}
                                                                      context:nil];
    UILabel *numberOfPropertiesLabel = [[UILabel alloc]initWithFrame:CGRectMake(priceLabel.frame.size.width, 0, numberOfPropertiesLabelRect.size.width+10, numberOfPropertiesLabelRect.size.height+12)];
    numberOfPropertiesLabel.text = marker.snippet;
    numberOfPropertiesLabel.textAlignment = NSTextAlignmentCenter;
    numberOfPropertiesLabel.textColor = [UIColor whiteColor];
    numberOfPropertiesLabel.backgroundColor = [UIColor clearColor];
    numberOfPropertiesLabel.font = [UIFont systemFontOfSize:11];

    self.frame = CGRectMake(0, 0, priceLabel.frame.size.width+numberOfPropertiesLabel.frame.size.width, priceLabel.frame.size.height+TriangleHeight);

    [self addSubview:priceLabel];
    [self addSubview:numberOfPropertiesLabel];


    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [[UIScreen mainScreen] scale]);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * icon = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return icon;
}

In the above code, 2 labels are creates priceLabel and numberOfPropertiesLabel . The frame of both the labels are set according to your requirement ie the position of the labels in the view. Then the frame of the view is set according to the labels' dimensions.

View is then converted into an image. This image is then set as the GMSMarker image.

You cannot select multiple markers at a time.

You can use an alternative approach instead.

You can create custom markers ie, the custom marker image can be created such that it contains the information/format you want to display in the info window.

The below image can give you an idea on how to achieve it:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM