1

I am trying to show a custom overlay view from the Google Maps through the Google Maps API for iOS. This overlay view content info of marker, and two my button. But I cannot find any references for this. My desired overlay view is as image with link below:

http://i1121.photobucket.com/albums/l515/dungnlh_khtn07/NguoiMoCay/overlayinfoview_zps8110b7ed.jpg

Please give me a guide for this!

Saxon Druce
  • 17,406
  • 5
  • 50
  • 71
Dung Nguyen
  • 885
  • 1
  • 7
  • 7

5 Answers5

5

Possibly, as mentioned officially in documentation of Google Maps Android API, the below restriction regarding infowindows applies to Google Maps iOs SDK also :

Info window is not a live View, rather the view is rendered as an image onto the map. As a result, any listeners you set on the view are disregarded and you cannot distinguish between click events on various parts of the view. You are advised not to place interactive components — such as buttons, checkboxes, or text inputs — within your custom info window.

So bascially clicking on any part of the infowindow will trigger only "didTapWindowOfMarker"

tony m
  • 4,769
  • 1
  • 21
  • 28
2

The best way of achieving this is to pass your GMSMapView a custom view for your info windows via the GMSMapViewDelegate's mapView:markerInfoWindow: method.

To accomplish this, set yourself up as the delegate for your GMSMapView instance, like so:

self.mapView.delegate = self;

Then return a custom UIView when requested:

- (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(id<GMSMarker>)marker
{
    UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    customView.backgroundColor = [UIColor redColor];

    return customView;
}

You can find a little more information by looking in the GMSMapView header, which states:

/**
 * Called when a marker is about to become selected, and provides an optional
 * custom info window to use for that marker if this method returns a UIView.
 * If you change this view after this method is called, those changes will not
 * necessarily be reflected in the rendered version.
 *
 * The returned UIView must not have bounds greater than 500 points on either
 * dimension.  As there is only one info window shown at any time, the returned
 * view may be reused between other info windows.
 *
 * @return The custom info window for the specified marker, or nil for default
 */
ndg
  • 2,585
  • 2
  • 33
  • 58
2

OK, but can someone please clarify how to get a UIButton on that UIView to respond to touches? Like two buttons in Dung Nguyen Le Hoang's attached picture. For example, if I add a button like this:

- (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(id<GMSMarker>)marker
{
    UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    customView.backgroundColor = [UIColor redColor];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
    [button setFrame:CGRectMake(0, 0, 30, 30)];

    [customView addSubview:button];

    return customView;
}

and of course have buttonPressed

- (void)buttonPressed
{
    NSLog(@"Hello");
}

it doesn't get called. If I added the button as a subview to self.mapView then it appears on mapView, not InfoWindow and, of course, is responsive to touches. I suppose it's due to some unset frames, but can't quite get it. Can someone please clarify?

Vladimir Kolbas
  • 236
  • 2
  • 4
  • 4
    Afraid not as the SDK renders UIView as an overlay image and that can't be responsive. I've made another solution: In method -(BOOL)mapView:(GMSMapView *)mapView didTapMarker:(id)marker I add a view as subview representing my Info Window and remember marker's coordinate at a @property so that that UIView can be repositioned in didChangeCameraPosition if the user moves the view. That UIView is then dismissed from view in -(void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate method which is fired when the user taps elsewhere on the map. – Vladimir Kolbas May 01 '13 at 09:52
  • @VladimirKolbas's answer is right. I had solved the same problem. – Dalinaum Mar 17 '14 at 10:24
  • @Dalinaum have you got solve? Please provide some code, will be the great help. thanks in Adv. – Hardik Darji Mar 15 '19 at 14:26
  • @HardikDarji It's already 5 years ago. I don't have any code for that anymore. Sorry. – Dalinaum Mar 30 '19 at 07:20
1
  • (void)mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:(GMSMarker *)marker
Pang Ho Ming
  • 1,299
  • 10
  • 29
0

You can show AlertView or ActionSheet with two buttons when the user tap on the markerInfoContents

Musa almatri
  • 5,596
  • 2
  • 34
  • 33