我目前正在开发一个项目,我需要用户告诉哪里(在真实世界地图上)构建墙。iOS - 测量距离的最佳方式
问题:什么(您认为)是用户显示(/告诉)放置墙的最准确方式?
想法1我曾想过在地图上绘图,但那不会那么准确。
想法2我想到的另一件事是,用户应该把他们的手机放在墙的开始和结束。通过这种方式,应用程序可以使用CLLocationManager
在地图上打印位置,并测量两端之间的距离。
这是我试过我的想法的代码,但它并不是真的那么准确。
let locationManager = CLLocationManager()
var location1: CLLocation = CLLocation()
var location2: CLLocation = CLLocation()
override func viewDidLoad() {
super.viewDidLoad()
setup()
}
func setup() {
resett.isHidden = true
// Ask for Authorisation from the User.
self.locationManager.requestAlwaysAuthorization()
// For use in foreground
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
locationManager.activityType = .fitness
locationManager.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locValue:CLLocationCoordinate2D = manager.location!.coordinate
currentCord.text = "\(locValue.latitude) \(locValue.longitude)"
}
func measure(cord1: CLLocation, cord2: CLLocation) -> Float {
let distanceInMeters = cord1.distance(from: cord2) //result is in meters
return Float(distanceInMeters)
}
func calculateCoordinates(status: Int) {
let coordinate = calculate()
if status == 1 {
location2 = coordinate
let measured = measure(cord1: location1, cord2: location2)
print("\(measured) m")
displayLabel.text = "\(measured)m"
resett.isHidden = false
} else {
location1 = coordinate
}
}
func calculate() -> CLLocation {
let locValue:CLLocationCoordinate2D = locationManager.location!.coordinate
let coordinate = CLLocation(latitude: locValue.latitude, longitude: locValue.longitude)
return coordinate
}
@IBAction func FirstAction(_ sender: Any) {
button1.setTitle("Klar", for: .normal)
calculateCoordinates(status: 0)
}
@IBAction func SecondAction(_ sender: Any) {
button2.setTitle("Klar", for: .normal)
calculateCoordinates(status: 1)
}
提前感谢!
您需要提供更多信息并更加清楚,您面临的具体问题是什么? CLLocationManager,“测量距离”和“在地图上绘图”没有真正相关 – jalone
对不起,现在更新了问题。 –