2017-04-05 19 views
0

我正在使用應用程序來記錄散步,但我無法使多段線與其他應用中看起來一樣平滑,例如runkeeper。我嘗試過不同的activityType,desiredAccuracy等等。我已經在iPhone 5c,6s和7上進行了測試。它總是看起來像示例照片,它在露天錄製下沒有任何附近的建築物。有什麼我失蹤?在Swift中錄製CLLocationPoints時不準確

這是我viewDidLoadviewWillAppearviewDidAppearlocationManagerrendererFor overlay

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) 
} 

結果: And no, I didn't walk like that!

回答

0

嘗試設置distanceFilterk​CLDistance​Filter​None。這會通知你每一個動作,但會消耗最多的電池。

即使您在建築物外,仍然無法獲得完美的準確性。 如果我猜測其他健身應用程序假設你沒有穿過建築物和東西,所以他們調整你的路徑來匹配街道。

我知道Strava展示了我通過建築物和花園的路徑,因爲它們沒有達到完美的精確度。

0

這樣做是爲了獲得最佳的準確性,以便每次獲取您的位置。

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

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()}) 

}