ios - 快速在 Google map 中自定义标记

标签 ios swift google-maps gmsmapview

我试图在谷歌地图中点击图钉时显示我的自定义 View 。我在 xib 文件中为它设置了自定义 View ,并在 func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool { 的委托(delegate)方法中调用该文件。但是当我运行应用程序并点击标记时,它不会显示我的 View 。我怎样才能展示这个?这是我的自定义 Xib 文件的代码,

class MapMarkerWindow: UIView {

    @IBOutlet weak var saloonImage: UIImageView!
    @IBOutlet weak var nameLbl: UILabel!
    @IBOutlet weak var addressLbl: UILabel!
    @IBOutlet weak var descriptionLbl: UILabel!
    var spotData: NSDictionary?


    class func instanceFromNib() -> UIView {
        return UINib(nibName: "MapMarkerWindowView", bundle: nil).instantiate(withOwner: self, options: nil).first as! UIView     
    }
}

这是我在 VC 中显示 map 的代码,

@IBOutlet weak var customView: UIView!

var mapView : GMSMapView?
var locationManager = CLLocationManager()
private var infoWindow = MapMarkerWindow()
fileprivate var locationMarker : GMSMarker? = GMSMarker()

override func viewDidLoad() {
    super.viewDidLoad()

    self.infoWindow = loadNiB()
    GMSServices.provideAPIKey("AIzaSyBMppjEPlRBHUD4To2KqNLgmhu1QcxHg3g")

    let camera = GMSCameraPosition.camera(withLatitude: 31.516331, longitude: 74.342792, zoom: 6)

    mapView = GMSMapView.map(withFrame: .zero, camera: camera)
    view = mapView

    let states = [
        State(name: "Hafeez Center", long: 74.342792, lat: 31.516331, snippets: "Lahore"),
        State(name: "Kalma Chowk", long: 74.331553, lat: 31.504532, snippets: "Lahore"),
        // the other 51 states here...
    ]

    for state in states {
        let state_marker = GMSMarker()
        state_marker.position = CLLocationCoordinate2D(latitude: state.lat, longitude: state.long)
        state_marker.title = state.name
        state_marker.snippet = "Hey, this is \(state.snippets)"
        state_marker.map = mapView
    }

    self.mapView?.isMyLocationEnabled = true

    //Location Manager code to fetch current location
    self.locationManager.delegate = self
    self.locationManager.startUpdatingLocation()
}

func loadNiB() -> MapMarkerWindow {
    let infoWindow = MapMarkerWindow.instanceFromNib() as! MapMarkerWindow
    return infoWindow
}

//Location Manager delegates
private func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    let location = locations.last

    let camera = GMSCameraPosition.camera(withLatitude: (location?.coordinate.latitude)!, longitude: (location?.coordinate.longitude)!, zoom: 17.0)

    self.mapView?.animate(to: camera)

    //Finally stop updating location otherwise it will come again and again in this delegate
    self.locationManager.stopUpdatingLocation()

}
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
    var markerData : NSDictionary?
    if let data = marker.userData! as? NSDictionary {
        markerData = data
    }
    locationMarker = marker
    infoWindow.removeFromSuperview()
    infoWindow = loadNiB()
    guard let location = locationMarker?.position else {
        print("locationMarker is nil")
        return false
    }
    // Pass the spot data to the info window, and set its delegate to self
    infoWindow.spotData = markerData
    //infoWindow.delegate = self
    // Configure UI properties of info window
    infoWindow.alpha = 0.9
    infoWindow.layer.cornerRadius = 12
    infoWindow.layer.borderWidth = 2
    //infoWindow.infoButton.layer.cornerRadius = infoWindow.infoButton.frame.height / 2
    let saloonImage = markerData!["image"]!
    let name = markerData!["name"]!
    let address = markerData!["address"]!
    let description = markerData!["description"]!

    infoWindow.addressLbl.text = address as? String
    infoWindow.nameLbl.text = name as? String
    infoWindow.descriptionLbl.text = description as? String
    infoWindow.saloonImage.image = saloonImage as? UIImage
    //Offset the info window to be directly above the tapped marker
    infoWindow.center = mapView.projection.point(for: location)
    infoWindow.center.y = infoWindow.center.y - 82
    self.view.addSubview(infoWindow)
    return false
}

func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
    if (locationMarker != nil){
        guard let location = locationMarker?.position else {
            print("locationMarker is nil")
            return
        }
        infoWindow.center = mapView.projection.point(for: location)
        infoWindow.center.y = infoWindow.center.y - 82
    }
}

func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
    infoWindow.removeFromSuperview()
}

这就是我点击图钉时的样子。 enter image description here

最佳答案

从您的代码看来,加载 Nib 时出现问题。

请检查以下代码。

    func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
        self.tappedmarker = marker
        var point = mapView.projection.point(for: marker.position)
        point.y = point.y - 110
        let camera = mapView.projection.coordinate(for: point)
        let position = GMSCameraUpdate.setTarget(camera)
        mapView.animate(with: position)

        self.customMarker = CustomMarker.instancefromNib() as! CustomMarker
        customMarker.layer.cornerRadius = 10.0
        customMarker.clipsToBounds = true

        self.customMarker.center = mapView.projection.point(for: marker.position)
        self.customMarker.center.y = self.customMarker.center.y - 110
        self.view.addSubview(self.customMarker)
        self.customMarker.bringSubview(toFront: self.view)

        return true
    }

    func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
        if (tappedmarker != nil) {
            guard let location = tappedmarker?.position else {
                print("locationMarker is nil")
                return
            }
            customMarker.center = mapView.projection.point(for: location)
            customMarker.center.y = customMarker.center.y - 100
        }
    }

    func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
        if self.view.subviews .contains(self.customMarker) {
            self.customMarker.removeFromSuperview()
            return
        }
    }

这是来自Nib的实例:

   class CustomMarker: UIView {

    @IBOutlet weak var lblTitle: UILabel!

    class func instancefromNib() -> UIView {
        return UINib.init(nibName: "CustomMarker", bundle: nil).instantiate(withOwner: self, options: nil).first as! UIView
    }

}

这看起来像这样: enter image description here

希望这会有所帮助!

关于ios - 快速在 Google map 中自定义标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50641424/

相关文章:

Javascript:在循环内生成对象键并将对象数组插入每个对象?

ios - swift fatal error : Can't form Range with upperBound < lowerBound

ios - NSDate 在 Swift 中返回错误的日期

ios - 当用户看不到 map 时如何从 Google Maps for iOS 制作图像

ios - 将循环的 mp4 背景添加到 iOS 应用程序注册

swift - 按名称删除使用闭包语法创建的 NotificationCenter 观察器是否足够?

google-maps - 谷歌地图捕捉到不返回所有值的道路

ios - NSURLSession 未到达服务器

ios - Apple map 中使用的底部表单 UIKit 组件的名称是什么?

ios - 如何在 iPhone 中播放实时音频?