2017-06-16 126 views
1

我目前正在开发一个项目,我需要用户告诉哪里(在真实世界地图上)构建墙。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) 
    } 

提前感谢!

+1

您需要提供更多信息并更加清楚,您面临的具体问题是什么? CLLocationManager,“测量距离”和“在地图上绘图”没有真正相关 – jalone

+1

对不起,现在更新了问题。 –

回答

0

假设你的意思是一个真实世界的“墙”,以几厘米到几米的尺寸排列,iPhone GPS的位置实际上并不适应这种测量,是典型的最小误差覆盖区域5米

如果明确地询问地理坐标(具有非常高的准确性)是不可行的: 我宁愿要求用户明确指出尺寸,然后拖动地图上的对象(墙),在接近时尽可能地缩放可能的最终位置。

PS:有很多反正优化是可能的,从而增加了CLLocationManager的准确性,先用低accuracy所有过滤出的结果与手动走大horizzontal accuracy问题的一个接收之后。

+0

您是否认为可以先向用户询问尺寸,然后使用ARKit将墙放在地图上? –

+0

是的,我相当肯定,这不应该是一个问题,因为你有一些坚实的AR基础:)用户应该能够拖/移动/命令墙,它会被相应的透视。您最终可以使用该位置来近似墙的初始位置,然后让用户对其进行优化,如果这是您需要的。 – jalone

+1

我会尝试做一个演示。我会看看我完成后是否可以在这里发布github链接 –

0

不幸的答案是iPhone上的GPS不是很准确。如果你可以在32米半径范围内获得准确的位置,那么你的表现很好,而且这是一个空旷的地方,对天空有清晰的视线,并且没有无线电干扰。如果事情不太理想,结果可能会不太准确。

(苹果报告一个“水平精度”的阅读,在它的GPS定位结果,实际上是误差范围的半径。)

对于修筑城墙的目的32米,是根本不够好,而iPhone中的GPS无法可靠地做得更好。你真的需要测量设备。