首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Swift中记录CLLocationPoints时不准确

在Swift中记录CLLocationPoints时不准确
EN

Stack Overflow用户
提问于 2017-04-05 19:41:13
回答 2查看 178关注 0票数 0

我正在使用一个应用程序来记录漫步,但我不能让折线像在其他应用程序中看起来那样平滑,比如runkeeper。我尝试过不同的activityType,desiredAccuracy等等。我已经在iPhone 5c、6s和7上测试过了。它看起来总是像示例照片,它是在没有任何建筑物的情况下录制出来的。我是不是漏掉了什么?

这是我的viewDidLoadviewWillAppearviewDidAppearlocationManagerrendererFor overlay

代码语言:javascript
复制
import UIKit
import MapKit
import CoreLocation
...
override func viewDidLoad() {
    super.viewDidLoad()
    myLocations = []
    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.activityType = .fitness
    manager.distanceFilter = 5
}
override func viewWillAppear(_ animated: Bool) {
    //Map
    logMap.delegate = self
    logMap.mapType = MKMapType.standard

    manager.requestAlwaysAuthorization()
    manager.startUpdatingLocation()
    manager.allowsBackgroundLocationUpdates = true

    logMap.userTrackingMode = MKUserTrackingMode(rawValue: 1)!
}
override func viewDidAppear(_ animated: Bool) {

    let status = CLLocationManager.authorizationStatus()
    if status == .notDetermined || status == .denied || status == 
.authorizedWhenInUse {
        manager.requestAlwaysAuthorization()
        manager.requestWhenInUseAuthorization()
        manager.requestLocation()
    }else{
        manager.startUpdatingLocation()
        logMap.showsUserLocation = true
    }
    if status != .denied {
        manager.startUpdatingLocation()
    }
}
func locationManager(_ manager: CLLocationManager, 
didChangeAuthorization status: CLAuthorizationStatus) {
    if status == .authorizedWhenInUse {
        manager.requestLocation()
    }
    if status == .authorizedAlways {
        logMap.showsUserLocation = true
        logMap.mapType = MKMapType(rawValue: 0)!
        //logMap.userTrackingMode = MKUserTrackingMode(rawValue: 3)!
        manager.startUpdatingLocation()
    }
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations 
locations: [CLLocation]){

    if(isLoging==true){
        if(myLocations.count > 1){
            self.distance = self.distance + 
Int(locations[0].distance(from: myLocations.last!))
        }
            myLocations.append(locations[0])
            setDistance()
    }
    // paint line
    if (myLocations.count > 1){
        let sourceIndex = myLocations.count - 1
        let destinationIndex = myLocations.count - 2

        let c1 = myLocations[sourceIndex].coordinate
        let c2 = myLocations[destinationIndex].coordinate
        var a = [c1, c2]
        let polyline = MKPolyline(coordinates: &a, count: a.count)
        logMap.add(polyline)
    }
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    if overlay is MKPolyline {
        let polylineRenderer = MKPolylineRenderer(overlay: overlay)
        polylineRenderer.strokeColor = UIColor.blue
        polylineRenderer.lineWidth = 4
        return polylineRenderer
    }
    return MKPolylineRenderer(overlay: overlay)
}

结果:

EN

回答 2

Stack Overflow用户

发布于 2017-04-05 20:07:34

尝试将distanceFilter设置为k​CLDistance​Filter​None。这将通知您的每一个动作,但将消耗最多的电池。

即使你在建筑物的外面,你仍然不能得到完美的准确性。如果我要猜测其他健身应用程序,假设你不会跑过建筑物和东西,所以他们会调整你的路径以匹配街道。

我知道一个事实,斯特拉瓦在建筑物和花园中展示了我的道路,因为它们不能得到完美的准确性。

票数 0
EN

Stack Overflow用户

发布于 2017-04-06 13:14:13

这样做可以获得最佳的精确度,以便每次都能获得您的位置。

代码语言:javascript
复制
override func loadView() {

    print("loadView called")

    // Enable some map settings

    map.isMyLocationEnabled = true
    map.settings.myLocationButton = true
    map.settings.compassButton = true
    map.settings.scrollGestures = true
    map.settings.zoomGestures = true
    map.delegate = self

    view = map

}

override func viewDidLoad() {

    super.viewDidLoad()

    print("ViewDidLoad called")

    // Configuring location manager.

    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.requestWhenInUseAuthorization()
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.startUpdatingLocation()
    locationManager.startMonitoringSignificantLocationChanges()

}

在ViewController中添加代理: CLLocationManagerDelegate,GMSMapViewDelegate

代码语言:javascript
复制
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    print("locationManager function called")

    // Fetch current location coordinates

    let locValue:CLLocationCoordinate2D = (locationManager.location?.coordinate)!
    currentLatitude = locValue.latitude
    currentLongitude = locValue.longitude
    print("Current Location = \(currentLatitude!), \(currentLongitude!)")

    // Zoom to current location

    let target = CLLocationCoordinate2D(latitude: currentLatitude!, longitude: currentLongitude!)
    map.camera = GMSCameraPosition.camera(withTarget: target, zoom: 17)

    locationManager.stopUpdatingLocation()

    //DispatchQueue.main.asyncAfter(deadline: .now() + 1.75, execute: {self.webResponse()})

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43230175

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档