简体   繁体   中英

Swift GeoPoint get ALL users location ios

I am trying to make a app where everyone can see everyones location at the same map. I can't find any tutorials on how to retrieve ALL users location on the same map.

I have manage to make a script which uploads the users location into Parse with this script:

PFGeoPoint.geoPointForCurrentLocationInBackground {


            (geoPoint: PFGeoPoint?, Error : NSError?) -> Void in
   if let geoPoint = geoPoint{

        PFUser.currentUser()? ["location"] = geoPoint
    PFUser.currentUser()?.saveInBackground()

I have also manage to get the location of the current user.

Any one know how i can display everyones location, not just mine?

Thank you for your time and help. I am very new to Swift so let me know if i need to provide more information.

Here is my display code.

PFGeoPoint.geoPointForCurrentLocationInBackground {


            (geoPoint: PFGeoPoint?, Error : NSError?) -> Void in
   if let geoPoint = geoPoint{

        PFUser.currentUser()? ["location"] = geoPoint
    PFUser.currentUser()?.saveInBackground()
self.MapView?.showsUserLocation = true
      self.MapView?.delegate = self
        MapViewLocationManager.delegate = self
        MapViewLocationManager.startUpdatingLocation()
     self.MapView?.setUserTrackingMode(MKUserTrackingMode.Follow, animated: false)




  self.locationManager.requestAlwaysAuthorization()

        self.locationManager.requestWhenInUseAuthorization()

        if CLLocationManager.locationServicesEnabled(){

    self.locationManager.delegate = self
            self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
            self.locationManager.startUpdatingLocation()
        }





        func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocationManager]){
        var locValue:CLLocationCoordinate2D = (manager.location?.coordinate)!
        print("locations = \(locValue.latitude) \(locValue.longitude)")

Outside ViewWDidLoad

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {


        let location = locations.last
        let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)

        let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1))

        self.MapView?.setRegion(region, animated: true)

        self.locationManager.stopUpdatingLocation()



    }

To get all users location is not so simple as you think. The most time regular app is working in the background. In the background app can be in background or suspended or terminated state ( about states ). And you have to get location from any of these states.
How is it possible: you should enable Background Mode for location tracking for your app. To do it simpler - use significant location tracking for all users, but accuracy will be about 500m.
To get more accuracy (but more difficult implementation) you can send silent push notification to users ( about silent push ), it wakes up app from terminated or suspended states to background state, and now you can get and send users current location to your server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM