简体   繁体   中英

How to track facebook current user's location IOS/Android

Is there any way I can track a facebook's user's location in real time who is logged in my mobile app and has location services enabled and also has granted access to my app so as to make use of its location?

  1. Assuming a user X, and 3 linear points in space: A,B,C. X is traveling from A to C.

  2. Is there an SDK that would enable me to check X's real time location ( latitude + longitude ) at any given time while X is moving from A to C so as to create a dotted map(by dropping a pin on the map) with the user's location at every 10ms?

  3. Is this feasible given the fact that my device has a 4G internet connection?

I think you can use CLLocationManager to update user location after traveling a number of meters. I presume you are already having a CLLocationManager to update your location? you can save the dots in an array (starting with the location of A, ending with location of C). You can then draw a line using the dots. I believe Google Map API has a method for drawing line. There's an answer for that here:

Here is a link from SO

But for the sake of providing code, I will provide it for you in Swift 3.0 (the code in the link is in ObjC):

override func viewDidLoad() {
    super.viewDidLoad()
    //This is a dummy location, you'd add locations to it using the 
    //  func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
    let location:CLLocation = CLLocation(latitude: 200, longitude: 100)
    let locationArray:Array<CLLocation> = [location]
    let camera:GMSCameraPosition = GMSCameraPosition.camera(withLatitude: (locationArray.first?.coordinate.latitude)!, longitude: (locationArray.first?.coordinate.longitude)!, zoom: 2)
    //You can obtain the Lat and Long for above from the list of arrays of locations you saved
    //You can use the .first or .last on the array (I used first)

    let mapview:GMSMapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)

    let path:GMSMutablePath = GMSMutablePath()
    for nextLocation in locationArray {
        if locationArray.index(of: nextLocation) != 0 {
            //You dont want to use the first one as you've already done it
            //so you start with 1
            path.addLatitude(nextLocation.coordinate.latitude, longitude: nextLocation.coordinate.longitude)
        }
    }

    let polyline:GMSPolyline = GMSPolyline(path: path)
    polyline.strokeColor = UIColor.red
    polyline.strokeWidth = 2
    polyline.map = mapview
    self.view = mapview

    //I personally prefer view.addSubview(mapview)
}

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