Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tap on ios google map marker programatically or show info window of marker?

I am working with Google Maps iOS SDK with multiple markers which will show marker info window on marker tap. Below code works for multiple markers and marker displayed in the Map View as expected. Its also show the marker info window on marker click and change the marker icon on click.

-(UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)imarker
{
    UIView *infowindow = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 90, 45)];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 90, 45)];
    [label setFont:[UIFont fontWithName:@"TrebuchetMS-Bold" size:22]];
    infowindow.layer.cornerRadius = 5;
    infowindow.layer.borderWidth = 2;
    label.textAlignment = NSTextAlignmentCenter;
    label.text = @"Test";
    label.textColor=[UIColor greenColor];
    infowindow.backgroundColor=[UIColor whiteColor];
    [infowindow addSubview:label];
    return infowindow;    
} 

Now i want to trigger programatically click on marker or show info window on marker.

marker = [GMSMarker markerWithPosition:position];
marker.title = @"Name";
marker.snippet = @"Test";        
[self mapView:_mapView didTapMarker:marker];
[_mapView setSelectedMarker:marker];

Above code not working for me. It just move to location of marker does not show the info window. Is there any way to programatically click on marker and show info window?

like image 710
Sid Mhatre Avatar asked Dec 22 '16 12:12

Sid Mhatre


People also ask

How do you customize a marker in maps?

For adding a custom marker to Google Maps navigate to the app > res > drawable > Right-Click on it > New > Vector Assets and select the icon which we have to show on your Map. You can change the color according to our requirements. After creating this icon now we will move towards adding this marker to our Map.

How do I move a marker smoothly on Google Maps?

The transition() and moveMarker() are used to move marker smoothly on click on the Google map.

What is the Google Maps marker called?

The Google Maps pin is the inverted-drop-shaped icon that marks locations in Google Maps. The pin is protected under a U.S. design patent as "teardrop-shaped marker icon including a shadow".


1 Answers

Providing map to the marker is essential

For Obj c

GMSMarker *myMarkerAutomaticSnippet = [[GMSMarker alloc] init];
marker.position = <Your cordinates>;
marker.title = @"my Title";
marker.snippet = @"my Snippet";
marker.map = myCustomMapView;


[myCustomMapView setSelectedMarker:marker];

For Swift 3.0

    let myMarker = GMSMarker()
    myMarker.position = CLLocationCoordinate2DMake(80.0, 80.0)
    myMarker.title = "marker title"
    myMarker.snippet = "marker snippet"
    myMarker.map = customMap // Here custom map is your GMSMapView


   customMap.customMapView.selectedMarker = myMarker // This line is important which opens the snippet
like image 74
Manish Pathak Avatar answered Oct 21 '22 03:10

Manish Pathak