如何在iOS的谷歌地图中不需要点击标记就能显示信息窗口?

27

我是iOS开发新手,关于Google Maps iOS SDK中的标记信息窗口有些疑问。

据我了解,我们可以使用GMSMarkerOption创建带有信息窗口的标记。

GMSMarkerOption *myLocationOption = [GMSMarkerOption alloc];
myLocationOption .title = @"My Location";
myLocationOption .snippet = @"Lat:...., Lang:....";

[mapView addMarkerOption:myLocationOption];
根据以上代码,标记在地图视图中如预期所示。并且在Google地图上点击标记会显示“My Location”信息窗口,这很好。 请问是否有办法在用户进入自定义地图屏幕时以编程方式显示信息窗口?
8个回答

65

谷歌地图 SDK 发生了变化,现在更易于理解:

GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = coordinate;
marker.title = @"Location selected";
marker.snippet = @"Testing";
marker.map = mapView_;

//Show info window on map
[mapView_ setSelectedMarker:marker];

你现在可以使用 setSelectedMarker 方法来显示一个标记的信息窗口


我使用了上面的代码,但是只有当我点击标记时才显示信息窗口。有没有办法同时显示信息窗口和图钉? - Bandish Dave
@BandishDave,上面的代码应该可以实现你想要的功能。如果不行,可能是SDK又有了变化。 - estemendoza
@BandishDave - setSelectedMarker 在2014年12月26日(SDK v1.9.1)之后仍然可用。 - Islam

31
GMSMarkerOptions *myLocationOptions = [GMSMarkerOptions options];
myLocationOptions.title = @"My Location";
myLocationOptions.snippet = @"Lat:...., Lang:....";

mapView.selectedMarker = [mapView addMarkerWithOptions:myLocationOptions];

(请注意是“Options”,而不是“Option”)


11

Swift 3.0

func addMarker(_ location:CLLocation){
        var locationMarker: GMSMarker!
        if locationMarker != nil {
            locationMarker.map = nil
        }
        locationMarker = GMSMarker(position: location.coordinate)
        locationMarker.map = mapView
        locationMarker.appearAnimation = kGMSMarkerAnimationPop
        locationMarker.icon = GMSMarker.markerImage(with: UIColor.green)
        locationMarker.opacity = 0.85
        locationMarker.isFlat = true
        locationMarker.snippet = "My Location"
        mapView.selectedMarker=locationMarker

    }

以下是答案

mapView.selectedMarker=locationMarker

3

Swift 3

self.mapView.selectedMarker = marker

在 Swift 3 中,您可以使用 selectedMarker 打开代码片段。

如果您创建的标记类似于以下方式:

marker.position = CLLocationCoordinate2D(latitude: 34.1331168, longitude: -118.3550723)
marker.title = "My super place name"
marker.snippet = "Are you looking a place to play? This is your place! "
marker.appearAnimation = kGMSMarkerAnimationPop
marker.map = self.mapView

2

--> 它可以在未点击标记的情况下显示多个信息窗口。 您可以轻松定制它。

最初的回答

for i in 0..

        let dict = arrNearByPlacesArray.object(at: i) as? NSDictionary ?? [:]

        let lat = dict.object(forKey: "latitude") as? String ?? ""
         let long = dict.object(forKey: "longitude") as? String ?? ""
        let company_id = dict.object(forKey: "company_id") as? String ?? ""
        let totaljobs = dict.object(forKey: "totaljobs") as? String ?? ""


        let location = CLLocationCoordinate2D(latitude: Double(lat) ?? 0.0, longitude: Double(long) ?? 0.0)
        print("location: \(location)")
        let marker = GMSMarker()
        //marker.icon = UIImage(named: "maps")

        let viewData = Bundle.main.loadNibNamed("MarkerXibView", owner: self, options: nil)?.first as! MarkerXibView .      //UIView


        marker.iconView = viewData .      //UIView


        marker.position = location
        marker.accessibilityLabel  = company_id
        marker.map = vwGoogleMap

}


2
   // Below line will shows the infowindow for marker with out tapping on it
   [mapView setSelectedMarker:myLocationOptions]; // myLocationOptions is your desired GMSMarker to show Infowindow with out tapping .

愉快编码 :)


2

mMapView.selectedMarker = marker


2

GMSMarkerOptions已经被弃用。使用它可以帮助我在不需要点击的情况下显示信息窗口-

func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) {
    myMapView.selectedMarker = myGMSMarker
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接