Search code examples
iosdictionarymarker

how to display marker title for multiple markers without tapping on it in iOS swift like places in photo gallery in iOS


Im able to display info window of markers but can we display the info when the map loads without even tapping on marker?


Solution

  • Okay, since google maps currently don't provide this functionality by default here is a work around. When you create your marker manipulate it as follows:

    • First you create a view and add your label and image to it.

      UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 20)];
              // Set label properties as required including marker Name
      
      UIImageView *markerIconImageView = [[UIImageView alloc]initWithFrame:CGRectMake(35, 10, 30, 50)];
              // Set image properties as required including image which will be displayed as marker
      
      UIView *markerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
      [markerView addSubview:label];
      [markerView addSubview:markerIconImageView];
      
    • Then you create a marker and assign it the image of above view:

      GMSMarker *marker = [[GMSMarker alloc] init];
      marker.icon  = [self imageWithView:markerView];
      marker.position = coordinates for marker;
      marker.appearAnimation = kGMSMarkerAnimationPop;
      marker.map = your map;
      
    • For reference here is my function for converting view to image:

      -(UIImage *) imageWithView:(UIView *)view
      {
          UIGraphicsBeginImageContext(view.bounds.size);
          [view.layer renderInContext:UIGraphicsGetCurrentContext()];
          UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
          UIGraphicsEndImageContext();
          return image;
       }
      

    We have converted the view to image because. It takes up lesser space in memory.

    Hope this helps. And gives you an idea for the approach.