Swift中有两个函数正在调用

时间:2019-01-25 07:50:58

标签: ios swift google-maps google-cloud-firestore

GoogleMaps数据库获取位置后,我正在使用Firestore在屏幕上显示位置标记,但是问题是我有三个功能。

第一个功能是显示Google地图上的所有用户列表,我在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!)")
            }
 }

现在,我呈现了一个用户列表,我在其中传递所选的用户坐标以在GoogleMaps上显示标记。

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!)")
            }
}

我使用委托方法来传递所选的用户信息。

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函数,我在其中传递 userData 中的用户信息。

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
}

它正在GoogleMaps上显示所选用户的标记。现在问题是GMSMapViewDelegate的功能与上述两个功能相同,并且在地图上显示了两个功能的标记。但我只想在地图上显示所选的用户信息。红色标记显示所选的用户位置。我该怎么办?

enter image description here

1 个答案:

答案 0 :(得分:0)

只需放置一个布尔标志,当您选择用户时将其设置为true并在委托中将其选中,然后清除地图叠加层并仅放置标记即可