iOS& Swift:显示当前位置的距离?

时间:2016-06-15 01:55:12

标签: ios iphone swift listview mapkit

我已经搜索了interwebs和stackoverflow,我无法找到解决问题的方法。

我正在尝试:

  1. 获取用户的当前位置(lat& long)
  2. 计算用户当前位置与我在内部设置的其他位置(lat& long)之间的距离
  3. 在列表视图中返回距离
  4. 到目前为止,如果我手动设置当前位置,我可以完成此操作,但我需要更新。

    我已成功将当前位置(我将其设置为模拟器中的Apple总部)返回到日志中,但在实际的应用程序或模拟器中没有成功。

    这就是我所拥有的:

    import UIKit
    import CoreLocation
    import MapKit
    
    class ViewController: UITableViewController, CLLocationManagerDelegate, MKMapViewDelegate {
    
    
    
    
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
    
    
    var shops = [coffeeShop]()
    
    var locationManager = CLLocationManager()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
    
    
        // Ask for Authorisation from the User.
        self.locationManager.requestAlwaysAuthorization()
    
        // For use in foreground
        self.locationManager.requestWhenInUseAuthorization()
    
        if CLLocationManager.locationServicesEnabled() {
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.startUpdatingLocation()
        }
    
    
    
    
        loadShops()
    
    }
    
    
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let locValue:CLLocationCoordinate2D = manager.location!.coordinate
                print("locations = \(locValue.latitude) \(locValue.longitude)")
    
    }
    
    
    func loadShops() {
    
        let currentLocation = CLLocation()
        let currentLat = currentLocation.coordinate.latitude
        let currentLong = currentLocation.coordinate.longitude
    
    
        var myLocation = CLLocation(latitude: currentLat, longitude: currentLong)
    
    
        let shopLocation1 = CLLocation(latitude: 39.7886939, longitude: -86.1547275)
        let distance1 = myLocation.distanceFromLocation(shopLocation1) / 1000
        let shop1 = coffeeShop(location: distance1)!
    
    }
    

    此外,我在info.plist中设置了所有内容以及所有这些好东西。

    我该如何工作!? * 轻轻地哭泣 *

    提前感谢您的所有帮助!

1 个答案:

答案 0 :(得分:-1)

我能够使用以下代码来实现我所需要的。有时候你只需要在宇宙中把它放在那里让宇宙自己做出反应。谢谢大家的帮助!

        let currentLat = self.locationManager.location!.coordinate.latitude
        let currentLong = self.locationManager.location!.coordinate.longitude