代码之家  ›  专栏  ›  技术社区  ›  Wings

有两个函数正在调用Swift

  •  0
  • Wings  · 技术社区  · 7 年前

    我正在使用 GoogleMaps 从中获取位置后在屏幕上显示位置标记的步骤 Firestore

    viewDidLoad() 方法。

    func showListOfAllUsers() {
    
    for document in snapshot!.documents {
                    print(document.data())
                    let marker = GMSMarker()
    
                    self.location.append(Location(trackingData: document.data()))
    
                    print(self.location)
    
                     guard let latitude = document.data()["Latitude"] as? Double else { return }
                     guard let longitude = document.data()["longitude"] as? Double else { return }
    
                    marker.position = CLLocationCoordinate2D(latitude: latitude as! CLLocationDegrees , longitude: longitude as! CLLocationDegrees)
                    marker.map = self.mapView
                    marker.userData = self.location
                    marker.icon = UIImage(named: "marker")
                    bounds = bounds.includingCoordinate(marker.position)
                    print("Data stored in marker \(marker.userData!)")
                }
     }
    

    谷歌地图

    func getAllLocationOfSelectedUserFromFirestore() {
    for document in snapshot!.documents {
                    print(document.data())
                    let marker = GMSMarker()
    
                    self.location.append(Location(trackingData: document.data()))
    
                    print(self.location)
    
                     guard let latitude = document.data()["Latitude"] as? Double else { return }
                     guard let longitude = document.data()["longitude"] as? Double else { return }
    
                    marker.position = CLLocationCoordinate2D(latitude: latitude as! CLLocationDegrees , longitude: longitude as! CLLocationDegrees)
                    marker.map = self.mapView
                    marker.userData = self.location
                    bounds = bounds.includingCoordinate(marker.position)
                    print("Data stored in marker \(marker.userData!)")
                }
    }
    

    我使用delegate方法传递所选的用户信息。

    extension MapViewController: ShowTrackingSalesMenListVCDelegate {
    
    func didSelectedFilters(_ sender: ShowTrackingSalesMenListViewController, with userID: String) {
        self.selectedUserID = userID
        self.userLogButton.isHidden = false
        print("The selected UserID is \(selectedUserID)")
        self.getAllLocationOfSelectedUserFromFirestore()  // called here the second function
    }
    

    这里是 GMSMapViewDelegate 传递用户信息的函数 用户数据 .

    func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
        print("didTap marker")
        self.view.endEditing(true)
        self.mapView.endEditing(true)
        if let _ = self.activeMarker {
            self.infoWindowView.removeFromSuperview()
            self.activeMarker = nil
        }
    
        self.infoWindowView = MarkerInfoView()
        let point = mapView.projection.point(for: marker.position)
        self.infoWindowView.frame = CGRect(x: (point.x-(self.infoWindowView.width/2.0)), y: (point.y-(self.infoWindowView.height+25.0)), width: self.infoWindowView.width, height: self.infoWindowView.height)
        self.activeMarker = marker
    
        for mark in location {
            self.infoWindowView.storeNameLabel?.text = mark.name
        }
        print(self.infoWindowView.storeNameLabel?.text as Any)
        if let data = marker.userData as? [String:Any] {
            print(data)
            self.storeMapData = data
            print(self.storeMapData)
    
            var name = "N/A"
            if let obj = data["name"] as? String {
                name = obj
            }
    
    
        } else {
    
        }
        infoWindowView.delegate = self
        self.mapView.addSubview(self.infoWindowView)
    
        return true
    }
    

    它在屏幕上显示所选用户的标记 谷歌地图 . 现在的问题是 GMSMapViewDelegate公司 上述两个函数的函数相同,它在地图上显示两个函数的标记。但我只想在地图上显示选定的用户信息。显示所选用户位置的红色标记。我该怎么做?

    enter image description here

    1 回复  |  直到 7 年前
        1
  •  0
  •   Ahmed Nour    7 年前

    只需放置一个布尔标志,当您选择用户时,将其设置为true并在代理中选中它,然后清除地图覆盖并仅放置您的标记