core location - Trying to build a compass that points to a city using Swift 3 -


i have 2 arrows 2 images, 1 points north points north, city arrow never stays fixed. keeps fluctuating when don't move device. should solve problem ?

also, suspect error in timetogetthedirection function.

here code:

 import uikit  import corelocation   struct tokyolocationconstants {      static let citylat = 35.6895      static let citylong = 139.6917    }   class viewcontroller: uiviewcontroller, cllocationmanagerdelegate {        let obj1 = cllocationmanager()       var degrees: double = 0     override func viewdidload() {          obj1.startupdatinglocation()         obj1.startupdatingheading()  }    func locationmanager(_ manager: cllocationmanager, didupdatelocations locations: [cllocation]) {      timetogetthedirection(location: locations.last!)  }     func timetogetthedirection(location: cllocation) {      let lat1 = location.coordinate.latitude // current latitude     let long1 = location.coordinate.longitude // current longitude       // fromula direction city site:     // http://www.movable-type.co.uk/scripts/latlong.html      let differenceinlong = tokyolocationconstants.citylong - long1      let y = sin(differenceinlong) * cos(tokyolocationconstants.citylat)     let x = cos(lat1) * sin(tokyolocationconstants.citylat) - sin(lat1) * cos(tokyolocationconstants.citylat) * cos(differenceinlong)      let theangleinrad = atan2(y, x)     var theangleindeg = theangleinrad * (180 / m_pi)      if(theangleindeg < 0){         theangleindeg = -theangleindeg;     } // end of formulla      degrees = theangleindeg; // assign result of formula above global var used below }     func locationmanager(_ manager: cllocationmanager, didupdateheading newheading: clheading) {      var rad: double = 0       if newheading.trueheading > 0 {        // make 1st arrow points north          rad = newheading.trueheading * (m_pi / 180)        imageview.transform = cgaffinetransform(rotationangle: cgfloat(-rad))     }        // make 2nd arrow points city      let citydegreeinrad = (m_pi / 180 ) * degrees     let = (newheading.trueheading - (citydegreeinrad))      lineimageview.transform = cgaffinetransform(rotationangle: cgfloat(-something)) } 

try this

func locationmanager(_ manager: cllocationmanager, didupdatelocations locations: [cllocation]) {                      let location = cllocation()                      let userloc : cllocationcoordinate2d = location.coordinate                     self.caluserangle(here: userloc)                  }  func caluserangle(here: cllocationcoordinate2d) {                  var x = 0.0                 var y = 0.0                 var dellon = 0.0                  //tokio coords                 let tokiolat = 35.6895                 let tokiolon = 139.6917                  dellon = tokiolon - here.longitude                 y = sin(dellon) * cos(tokiolat);                 x = cos(here.latitude) * sin(tokiolat) - sin(here.latitude) * cos(tokiolat) * cos(dellon)                  let theangleinrad = atan2(y, x)                 var theangleindeg = theangleinrad * (180 / .pi)                  if(theangleindeg < 0){                     theangleindeg = -theangleindeg                 } else {                     theangleindeg = 360 - theangleindeg                 }                  //degress double var                 degress = theangleindeg             }          func locationmanager(_ manager: cllocationmanager, didupdateheading newheading: clheading) {                  print(string(format: "%.0f°", newheading.magneticheading))                  //convert degree radian , move needle                 let newrad =  -newheading.trueheading * double.pi/180                  //first arrow animation destination                 uiview.animate(withduration:0.3){                     self.imgarrowup.transform = cgaffinetransform(rotationangle: cgfloat((self.degress - newheading.trueheading) * .pi/180));                 }                  //second arrow animation north                 uiview.animate(withduration:0.6){                     self.compassview.transform = cgaffinetransform(rotationangle: cgfloat(newrad));                 }              } 

i hope i've helped


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -