title: How to display current speed in MPH based on data from GPS and Accelerometer. I have an idea, but having difficulty executing it
body: <p>I apologize for not knowing the proper terminology for everything here. I'm a fairly new programmer, and entirely new to Swift. The task I'm trying to accomplish is to display a current speed in MPH. I've found that using the "CoreLocation" and storing locations in an array and using "locations.speed "to display the speed is quite slow and does not refresh as often as I want. </p>
  
  <p>My thought was to get an initial speed value using the "MapKit" and "CoreLocation" method, then feed that initial speed value into a function using the accelerometer to provide a quicker responding speedometer. I would do this by integrating the accelerometer values and adding the initial velocity. This was the best solution I could come up with to get a more accurate speedometer with a better refresh rate.</p>
  
  <p>I'm having a couple of issues currently: </p>
  
  <p>First Issue: I don't know how to get an initial speed value from a function using location data as parameters into a function using accelerometer data as parameters. </p>
  
  <p>Second Issue: Even when assuming an initial speed of 0, my current program displays a value that keeps increasing infinitely. I'm not sure what the issue is that is causing this.</p>
  
  <p>I will show you the portion of my code responsible for this, and would appreciate any insight any of you may have!</p>
  
  <p>For my First Issue, here is my GPS data Function:</p>
  
  <pre><code>    func provideInitSpeed(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])-&gt;Double {
  
      let location = locations[0]
  
      return ((location.speed)*2.23693629) //returns speed value converted to MPH
  }
  </code></pre>
  
  <p>I'm not sure how to make a function call to retrieve this value in my Accelerometer function.</p>
  
  <p>For my Second Issue, here is my Accelerometer Function with assumed starting speed of 0:</p>
  
  <pre><code>motionManager.startAccelerometerUpdates(to: OperationQueue.current!) {
          (data,error) in
          if let myData = data {
              //getting my acceleration data and rounding the values off to the hundredths place to reduce noise
              xAccel = round((myData.acceleration.x * g)*100)/100
              yAccel = round((myData.acceleration.y * g)*100)/100
              zAccel = round((myData.acceleration.z * g)*100)/100
  
              // Integrating accel vals to get velocity vals *Possibly where error occurs* I multiply the accel values by the change in time, which is currently set at 0.2 seconds.
              xVel += xAccel * self.motionManager.accelerometerUpdateInterval
              yVel += yAccel * self.motionManager.accelerometerUpdateInterval
              zVel += zAccel * self.motionManager.accelerometerUpdateInterval
  
              // Finding total speed; Magnitude of Velocity
              totalSpeed = sqrt(pow(xVel,2) + pow(yVel,2) + pow(zVel,2))
  
              // if-else statment for further noise reduction. note: "zComp" just adjusts for the -1.0 G units z acceleration value that the phone reads by default for gravity
  
              if (totalSpeed - zComp) &gt; -0.1 &amp;&amp; (totalSpeed - zComp) &lt; 0.1 {
                  self.view.reloadInputViews()
                  self.speedWithAccelLabel.text = "\(0.0)"
              } else {
                  // Printing totalSpeed
                  self.view.reloadInputViews()
                  self.speedWithAccelLabel.text = "\(abs(round((totalSpeed - zComp + /*where initSpeed would go*/)*10)/10))"
              }
  
          }//data end
      }//motionManager end
  </code></pre>
  
  <p>I'm not sure why but the speed this function displays is always increasing by about 4 mph every refresh of the label.</p>
  
  <p>This is my first time using Stack Overflow, so I apologize for any stupid mistakes I might have made!</p>
  
  <p>Thanks a lot!</p>