Swift 3显示Firebase所有用户在地图上的个人资料图片作为注释

时间:2018-01-02 01:18:23

标签: swift firebase mapkit core-location

我想在地图上显示用户的个人资料照片。我在Firebase上将数据存储为Firebase上的经度,纬度,照片作为网址。现在,我正在为控制台上的所有用户获取数据。但我无法将这些数据显示为注释。个人资料照片将根据经度和纬度在地图上显示。我怎样才能做到这一点。我搜索了很多网站和很多教程。我找不到任何东西。所有显示在地图上添加一张图片作为注释。我想为所有用户这样做。

    class HomeTest: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

    @IBOutlet weak var mapView: MKMapView!
    var ref = Database.database().reference()
    var storageRef = Storage.storage().reference()
    let manager = CLLocationManager()
    var users = [User] ()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.mapView.delegate = self
        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()
        fetchUser()

        }

    func fetchUser() {
        Database.database().reference().child("locations").observe(.childAdded, with: {(snapshot) in

            if let dictionary = snapshot.value as? [String: AnyObject] {

                let user = User()

                user.latitude = dictionary["latitude"] as! Float
                user.longitude = dictionary["longitude"] as! Float
                user.photo = dictionary["photo"] as! String
                //user.username = dictionary["username"] as! String

               print(user.latitude, user.longitude, user.photo)

                self.users.append(user)

                DispatchQueue.main.async(execute: {
                   // ???
                })
            }

        } , withCancel: nil)

    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let location = locations [0]

        let myLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
        let span:MKCoordinateSpan = MKCoordinateSpanMake(0.020,0.020)

        let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation,span)
        self.mapView.setRegion(region, animated: true)
        self.mapView.isZoomEnabled = true
        self.mapView.isScrollEnabled = true
        self.mapView.showsUserLocation = true
    }
}

0 个答案:

没有答案