ios - 如何以编程方式点击 ios google map 标记或显示标记的信息窗口?

标签 ios objective-c google-maps gmsmapview

我正在使用带有多个标记的 Google Maps iOS SDK,这些标记将在标记点击时显示标记信息窗口。下面的代码适用于多个标记和按预期显示在 map View 中的标记。它还会在标记点击时显示标记信息窗口,并在点击时更改标记图标。

-(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;    
} 

现在我想以编程方式触发点击标记或在标记上显示信息窗口。

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

以上代码对我不起作用。它只是移动到标记的位置不显示信息窗口。有没有办法以编程方式点击标记并显示信息窗口?

最佳答案

为标记提供 map 是必不可少的

对于 objective-c

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


[myCustomMapView setSelectedMarker:marker];

对于 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

关于ios - 如何以编程方式点击 ios google map 标记或显示标记的信息窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41283642/

相关文章:

ios - 首次启动时从 iOS 应用程序中的转储文件创建数据库

Javascript 和 Actionscript 3 [设置变量问题]

javascript - 从外部访问二维数组中的 Google map 标记

php - 通过移动应用程序为二维码的值添加前缀

iphone - 从网站订购多个异步 JSON 解析

ios - 如何修复与 sizeWithFont 和 drawAtPoint 相关的折旧警告?

ios - 我可以使用自定义排序逻辑在 Swift 中对数组进行排序吗

google-maps - Google Map API v3 上的标记偏移

iOS 无法找到插件,Android 正常

ios - Apple Mach-O 链接器 (id) 错误 pjsip