0

I want to show user's profile pictures on map. I am storing datas on Firebase as longitude, latitude , photo as url on Firebase. Now, I am getting datas for all users on console. But I could not show these datas as annotation. Profile pictures will show on map according to longitude and latitude. How can I do this. I searched lots of websites and lots of tutorials. I could not find anything. All of shows that adding one picture on map as annotation. I want to do this for all users.`

    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
    }
}
KENdi
  • 7,576
  • 2
  • 16
  • 31
  • Sorry, I cannot give a detailed answer, but you can follow this link, it can help you https://digitalleaves.com/blog/2016/12/building-the-perfect-ios-map-ii-completely-custom-annotation-views/. The idea is run a loop to add all `annotations`, then give the custom view for annotation in `mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation)`. With that custom view, you can add everything, avatar, name... – t4nhpt Jan 02 '18 at 02:30
  • also the answers in this question were of great help to me: https://stackoverflow.com/questions/25631410/swift-different-images-for-annotation – Peter de Vries Jan 08 '18 at 08:21

0 Answers0