【发布时间】:2016-06-15 01:55:12
【问题描述】:
我搜索了互联网和 stackoverflow,但找不到解决问题的方法。
我正在尝试:
- 获取用户的当前位置(纬度和经度)
- 计算用户当前位置与我在内部设置的另一个位置(纬度和经度)之间的距离
- 在列表视图中返回距离
到目前为止,如果我手动设置当前位置,我可以做到这一点,但我需要更新。
我已经成功在日志中返回了我当前的位置(我在模拟器中设置为苹果总部),但在实际的应用程序或模拟器中没有成功。
这是我所拥有的:
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 中设置了所有内容以及所有这些好东西。
我该如何进行这项工作!? * 轻轻地哭泣 *
提前感谢您的所有帮助!
【问题讨论】:
-
哪个部分不工作?你有当前位置检测工作吗?回复
@JohnRamos. -
您不需要两次请求位置权限。
-
你只在模拟器中试过吗?如果是这样,您是否打开了模拟器的虚假位置?
-
let currentLocation = CLLocation()无法获取您的当前位置。它创建了一个坐标为 0,0 的新位置对象。您需要使用您在委托方法中获得的位置didUpdateLocations -
@JohnRamos 不工作的部分在 loadShops() myLocation 没有拉我的实际位置。
标签: ios iphone swift listview mapkit