Search code examples
firebaseswift3mkmapviewclcircleregion

Show Venues in Firebase When User is near


I'm trying to have my map display venues, pulled from Firebase, that are within a half mile radius from my user but, I'm not quite there yet. Could anyone assist?

This is what I'm doing now:

1) Populate the map and tableView with the data from my Firebase Database in my viewDidLoad:

var posts = [Post]()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Set up Map
        mapView.delegate = self
        mapView.userTrackingMode = MKUserTrackingMode.follow

        // Setup TableView
        tableView.delegate = self
        tableView.dataSource = self

        //Pulls TableData for UITableView
        DataService.ds.REF_VENUE.observe(.value, with: { (snapshot) in

            self.posts = [] // THIS IS THE NEW LINE
            if let snapshot = snapshot.children.allObjects as? [DataSnapshot] {
                for snap in snapshot {
                    if let postDict = snap.value as? Dictionary<String, AnyObject> {
                        let key = snap.key
                        let post = Post(postKey: key, postData: postDict)
                        self.posts.append(post)
                    }

                    //Populates Map with annotations.
                    if let locationDict = snap.value as? Dictionary<String, AnyObject> {

                        let lat = locationDict["LATITUDE"] as! CLLocationDegrees
                        let long = locationDict["LONGITUDE"] as! CLLocationDegrees
                        let title = locationDict["NAME"] as! String
                        let center = CLLocationCoordinate2D(latitude: lat, longitude: long)
                        _ = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.20, longitudeDelta: 0.20))

                        let annotation = MKPointAnnotation()
                        annotation.coordinate = CLLocationCoordinate2DMake(lat, long)
                        annotation.title = title.capitalized
                        self.mapView.addAnnotation(annotation)


                    }
                }
            }
            self.tableView.reloadData()
        })
    }

2) Then my tableView is set up normally: posts[indexPath.row], pulling from class that was specifically tailored for it.

But, back to the problem, all locations show up. The map is overpopulated with all venues. Could anyone help!!!


Solution

  • So I can't guide you through the code, since I'm not a IOS developer. But to achieve this you would need to create some kind of geolocation queries.

    In your case you want to filter data based on their radius. (circular query)

    To achieve this you need to have a center (probably the location of your user or the the result of a locations search) and the distance of each location to the center.

    If you have this, then the query would look like this:

    if (distance <= radius) {
      // location is in radius
    }
    

    I've just built a javaScript library for this so maybe this helps you (you can see there how to calculate the distance between two locations): https://github.com/Orlandster1998/geo-on-fire/blob/master/src/gof-utils.js#L215

    or here the docs: https://github.com/Orlandster1998/geo-on-fire/wiki/Basic-usage#query-by-radius

    Now if you have a lot of data to query you should definitely go a more advanced approach, with some kind of geohashing.