简体   繁体   English

如何以编程方式点击ios谷歌地图标记或显示标记的信息窗口?

[英]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. 我正在使用带有多个标记的Google Maps iOS SDK,它会在标记点按上显示标记信息窗口。 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? 有没有办法以编程方式点击标记并显示信息窗口?

Providing map to the marker is essential 提供标记的地图是必不可少的

For Obj c 对于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 对于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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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