简体   繁体   中英

Passing data between mapView and tableView

I have a viewController with a mapView and a tableView on it. I'm trying to get data like the users current location from the mapView and be able to pass it to the tableView so I can print out the calculated distance.

I tried using this code...

let distance = currentLocation.distanceFromLocation(endLocation)

But this doesn't work because currentLocation is created in locationManager's didUpdateLocation function, while endLocation is from the tableview data that I"m pulling from CloudKit. I can't use the two pieces of data together because they are from different functions.

How do I go about setting the userLocation so that I can use it outside the locationManager's function?

I have this..

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    let userLocation:CLLocation = locations[0] as! CLLocation
    let ulong = userLocation.coordinate.longitude;
    let ulat = userLocation.coordinate.latitude;
}

But I can't use the data outside that function.

Save your current location and end location to different CLLocation objects and then implement following code.

func CalculateDisatnceBetweenTwoLocations(source:CLLocation,destination:CLLocation) -> Double{

var distanceMeters = source.distanceFromLocation(destination)
var distanceKM = distanceMeters / 1000
let roundedTwoDigit = distanceKM.roundedTwoDigit
return roundedTwoDigit

}

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