谷歌地图iOS swift中的自定义标记InfoWindow?

时间:2017-02-23 11:01:36

标签: swift storyboard google-maps-markers google-maps-sdk-ios infowindow

我在我的应用中使用谷歌地图iOS,我想显示自定义标记信息窗口。如何创建它? 我使用故事板和swift作为语言。

enter image description here

2 个答案:

答案 0 :(得分:4)

我的应用程序遇到了这样的问题。

  1. 在ViewController.swift中:

     class ViewController: GMSMapViewDelegate {
            func mapView(_ mapView: GMSMapView, markerInfoContents marker: GMSMarker) -> UIView? {
    
                    let placeMarker = marker as! PlaceMarker
    
                    if let infoView = UIView.viewFromNibName("MarkerInfoView") as? MarkerInfoView {
                        infoView.nameLabel.text = placeMarker.place.name
    
                        if let photo = placeMarker.place.photo {
                            infoView.placePhoto.image = photo
                        } else {
                            infoView.placePhoto.image = UIImage(named: "generic")
                        }
    
                        return infoView
                    } else {
                        return nil
                    }
                }
            }
    
  2. 2.然后创建一个UIView类:

    import UIKit
    
    class MarkerInfoView: UIView {
    
        @IBOutlet weak var placePhoto: UIImageView!
        @IBOutlet weak var nameLabel: UILabel!
    }
    
    1. 然后创建一个名为与UIView类相同的xib文件:
    2. Like this

      请注意,类名与UIView类相同。您可以根据需要添加标签和图像。将这些IBOutlet连接到UIView类,如图所示。

      希望这会对你有所帮助。一切都好!

答案 1 :(得分:0)

为您的 public class XYZClass { public string Name { get; set; } } public class Tests { public void NormalBehavior() { var @ref = new WeakReference<XYZClass>(new XYZClass { Name = "bleh" }); GC.Collect(); GC.WaitForPendingFinalizers(); XYZClass t; @ref.TryGetTarget(out t); Console.WriteLine(t == null); //outputs true } public void WeirdBehavior() { var @ref = new WeakReference<XYZClass>(new XYZClass { Name = "bleh" }); GC.Collect(); GC.WaitForPendingFinalizers(); XYZClass t; @ref.TryGetTarget(out t); Console.WriteLine(t == null); //outputs false Console.WriteLine(t?.Name == null); //outputs false } } 定义委托,并在您的委托中实施此功能:

  

func mapView(_ mapView:GMSMapView,markerInfoWindow marker:GMSMarker) - &gt; UIView的? {   }

您可以在此函数内以编程方式创建视图,也可以通过nib文件创建视图。

<强>更新

不要忘记更改标记的infoWindowAnchor: https://developers.google.com/maps/documentation/ios-sdk/reference/interface_g_m_s_marker.html#a6668957ca7b6857355ee6cd9ab944a92