Add Coordinates (from UIViewController) to Constants File











up vote
0
down vote

favorite












I pulled the coordinates from the user in my main view controller like this:



import CoreLocation

private let locationManager = CLLocationManager()

func findCurrentLocation() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()

if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
//locationManager.startUpdatingHeading
}
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
print("locations = (locValue.latitude) (locValue.longitude)")
}


I then have this URL in a separate file (my constants file)



let NEAREST_CITY_URL = BASE_URL + "nearest_city?lat={{LATITUDE}}&lon={{LONGITUDE}}&key=" + API_KEY


I need to get the latitude and longitude from the view controller into that URL. How would I pass it there?



I assume it needs to look something like this, but I can't figure out how to compile it without errors.



let NEAREST_CITY_URL = BASE_URL + "nearest_city?lat=(MainVC.locationManager.locValue.latitude)&lon=(MainVC.locationManager.locValue.longitude)&key=" + API_KEY









share|improve this question




























    up vote
    0
    down vote

    favorite












    I pulled the coordinates from the user in my main view controller like this:



    import CoreLocation

    private let locationManager = CLLocationManager()

    func findCurrentLocation() {
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestWhenInUseAuthorization()

    if CLLocationManager.locationServicesEnabled() {
    locationManager.startUpdatingLocation()
    //locationManager.startUpdatingHeading
    }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
    print("locations = (locValue.latitude) (locValue.longitude)")
    }


    I then have this URL in a separate file (my constants file)



    let NEAREST_CITY_URL = BASE_URL + "nearest_city?lat={{LATITUDE}}&lon={{LONGITUDE}}&key=" + API_KEY


    I need to get the latitude and longitude from the view controller into that URL. How would I pass it there?



    I assume it needs to look something like this, but I can't figure out how to compile it without errors.



    let NEAREST_CITY_URL = BASE_URL + "nearest_city?lat=(MainVC.locationManager.locValue.latitude)&lon=(MainVC.locationManager.locValue.longitude)&key=" + API_KEY









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I pulled the coordinates from the user in my main view controller like this:



      import CoreLocation

      private let locationManager = CLLocationManager()

      func findCurrentLocation() {
      locationManager.delegate = self
      locationManager.desiredAccuracy = kCLLocationAccuracyBest
      locationManager.requestWhenInUseAuthorization()

      if CLLocationManager.locationServicesEnabled() {
      locationManager.startUpdatingLocation()
      //locationManager.startUpdatingHeading
      }
      }

      func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
      guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
      print("locations = (locValue.latitude) (locValue.longitude)")
      }


      I then have this URL in a separate file (my constants file)



      let NEAREST_CITY_URL = BASE_URL + "nearest_city?lat={{LATITUDE}}&lon={{LONGITUDE}}&key=" + API_KEY


      I need to get the latitude and longitude from the view controller into that URL. How would I pass it there?



      I assume it needs to look something like this, but I can't figure out how to compile it without errors.



      let NEAREST_CITY_URL = BASE_URL + "nearest_city?lat=(MainVC.locationManager.locValue.latitude)&lon=(MainVC.locationManager.locValue.longitude)&key=" + API_KEY









      share|improve this question















      I pulled the coordinates from the user in my main view controller like this:



      import CoreLocation

      private let locationManager = CLLocationManager()

      func findCurrentLocation() {
      locationManager.delegate = self
      locationManager.desiredAccuracy = kCLLocationAccuracyBest
      locationManager.requestWhenInUseAuthorization()

      if CLLocationManager.locationServicesEnabled() {
      locationManager.startUpdatingLocation()
      //locationManager.startUpdatingHeading
      }
      }

      func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
      guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
      print("locations = (locValue.latitude) (locValue.longitude)")
      }


      I then have this URL in a separate file (my constants file)



      let NEAREST_CITY_URL = BASE_URL + "nearest_city?lat={{LATITUDE}}&lon={{LONGITUDE}}&key=" + API_KEY


      I need to get the latitude and longitude from the view controller into that URL. How would I pass it there?



      I assume it needs to look something like this, but I can't figure out how to compile it without errors.



      let NEAREST_CITY_URL = BASE_URL + "nearest_city?lat=(MainVC.locationManager.locValue.latitude)&lon=(MainVC.locationManager.locValue.longitude)&key=" + API_KEY






      swift api url






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 5:52









      rmaddy

      237k27308375




      237k27308375










      asked Nov 10 at 5:48









      burgoyne

      307




      307
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          MainVC needs to set the data into your constants file, as a global variable (since you seem to desire using globals... eek). Then you can offer a NEAREST_CITY_URL that computes a string using that data.



          In your constants file:



          var userLoc : CLLocationCoordinate2D?

          let NEAREST_CITY_URL = BASE_URL + "nearest_city?lat=(userLoc.latitude ?? 0.0)&lon=(userLoc.longitude ?? 0.0)&key=" + API_KEY


          In your view controller:



          func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
          guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
          print("locations = (locValue.latitude) (locValue.longitude)")
          userLoc = locValue
          }


          Now it's really bad to have a global constants file like you're doing... at the very least, place all your constants into a singleton class named Constants. But I'm just here to directly answer your question, so...






          share|improve this answer























          • Thank you. I'm fairly new to Swift, but I will look into refactoring my code to use a Singleton class.
            – burgoyne
            Nov 10 at 6:15










          • So adding that to my code, it still prints out my correct coordinates, but it still wasn't working. So I printed the NEAREST_CITY_URL and it showed the coordinates as 0.0, and 0.0. Any idea why this would be?
            – burgoyne
            Nov 10 at 17:59










          • Have you tried debugging it? Set a breakpoint at userLoc = locValue and see if stepping over it results in the userLoc changing. You can also add a didSet to userLoc and add a breakpoint there to see whenever it changes.
            – Smartcat
            Nov 13 at 17:48











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236342%2fadd-coordinates-from-uiviewcontroller-to-constants-file%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote













          MainVC needs to set the data into your constants file, as a global variable (since you seem to desire using globals... eek). Then you can offer a NEAREST_CITY_URL that computes a string using that data.



          In your constants file:



          var userLoc : CLLocationCoordinate2D?

          let NEAREST_CITY_URL = BASE_URL + "nearest_city?lat=(userLoc.latitude ?? 0.0)&lon=(userLoc.longitude ?? 0.0)&key=" + API_KEY


          In your view controller:



          func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
          guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
          print("locations = (locValue.latitude) (locValue.longitude)")
          userLoc = locValue
          }


          Now it's really bad to have a global constants file like you're doing... at the very least, place all your constants into a singleton class named Constants. But I'm just here to directly answer your question, so...






          share|improve this answer























          • Thank you. I'm fairly new to Swift, but I will look into refactoring my code to use a Singleton class.
            – burgoyne
            Nov 10 at 6:15










          • So adding that to my code, it still prints out my correct coordinates, but it still wasn't working. So I printed the NEAREST_CITY_URL and it showed the coordinates as 0.0, and 0.0. Any idea why this would be?
            – burgoyne
            Nov 10 at 17:59










          • Have you tried debugging it? Set a breakpoint at userLoc = locValue and see if stepping over it results in the userLoc changing. You can also add a didSet to userLoc and add a breakpoint there to see whenever it changes.
            – Smartcat
            Nov 13 at 17:48















          up vote
          1
          down vote













          MainVC needs to set the data into your constants file, as a global variable (since you seem to desire using globals... eek). Then you can offer a NEAREST_CITY_URL that computes a string using that data.



          In your constants file:



          var userLoc : CLLocationCoordinate2D?

          let NEAREST_CITY_URL = BASE_URL + "nearest_city?lat=(userLoc.latitude ?? 0.0)&lon=(userLoc.longitude ?? 0.0)&key=" + API_KEY


          In your view controller:



          func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
          guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
          print("locations = (locValue.latitude) (locValue.longitude)")
          userLoc = locValue
          }


          Now it's really bad to have a global constants file like you're doing... at the very least, place all your constants into a singleton class named Constants. But I'm just here to directly answer your question, so...






          share|improve this answer























          • Thank you. I'm fairly new to Swift, but I will look into refactoring my code to use a Singleton class.
            – burgoyne
            Nov 10 at 6:15










          • So adding that to my code, it still prints out my correct coordinates, but it still wasn't working. So I printed the NEAREST_CITY_URL and it showed the coordinates as 0.0, and 0.0. Any idea why this would be?
            – burgoyne
            Nov 10 at 17:59










          • Have you tried debugging it? Set a breakpoint at userLoc = locValue and see if stepping over it results in the userLoc changing. You can also add a didSet to userLoc and add a breakpoint there to see whenever it changes.
            – Smartcat
            Nov 13 at 17:48













          up vote
          1
          down vote










          up vote
          1
          down vote









          MainVC needs to set the data into your constants file, as a global variable (since you seem to desire using globals... eek). Then you can offer a NEAREST_CITY_URL that computes a string using that data.



          In your constants file:



          var userLoc : CLLocationCoordinate2D?

          let NEAREST_CITY_URL = BASE_URL + "nearest_city?lat=(userLoc.latitude ?? 0.0)&lon=(userLoc.longitude ?? 0.0)&key=" + API_KEY


          In your view controller:



          func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
          guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
          print("locations = (locValue.latitude) (locValue.longitude)")
          userLoc = locValue
          }


          Now it's really bad to have a global constants file like you're doing... at the very least, place all your constants into a singleton class named Constants. But I'm just here to directly answer your question, so...






          share|improve this answer














          MainVC needs to set the data into your constants file, as a global variable (since you seem to desire using globals... eek). Then you can offer a NEAREST_CITY_URL that computes a string using that data.



          In your constants file:



          var userLoc : CLLocationCoordinate2D?

          let NEAREST_CITY_URL = BASE_URL + "nearest_city?lat=(userLoc.latitude ?? 0.0)&lon=(userLoc.longitude ?? 0.0)&key=" + API_KEY


          In your view controller:



          func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
          guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
          print("locations = (locValue.latitude) (locValue.longitude)")
          userLoc = locValue
          }


          Now it's really bad to have a global constants file like you're doing... at the very least, place all your constants into a singleton class named Constants. But I'm just here to directly answer your question, so...







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 at 6:02

























          answered Nov 10 at 5:56









          Smartcat

          1,6741315




          1,6741315












          • Thank you. I'm fairly new to Swift, but I will look into refactoring my code to use a Singleton class.
            – burgoyne
            Nov 10 at 6:15










          • So adding that to my code, it still prints out my correct coordinates, but it still wasn't working. So I printed the NEAREST_CITY_URL and it showed the coordinates as 0.0, and 0.0. Any idea why this would be?
            – burgoyne
            Nov 10 at 17:59










          • Have you tried debugging it? Set a breakpoint at userLoc = locValue and see if stepping over it results in the userLoc changing. You can also add a didSet to userLoc and add a breakpoint there to see whenever it changes.
            – Smartcat
            Nov 13 at 17:48


















          • Thank you. I'm fairly new to Swift, but I will look into refactoring my code to use a Singleton class.
            – burgoyne
            Nov 10 at 6:15










          • So adding that to my code, it still prints out my correct coordinates, but it still wasn't working. So I printed the NEAREST_CITY_URL and it showed the coordinates as 0.0, and 0.0. Any idea why this would be?
            – burgoyne
            Nov 10 at 17:59










          • Have you tried debugging it? Set a breakpoint at userLoc = locValue and see if stepping over it results in the userLoc changing. You can also add a didSet to userLoc and add a breakpoint there to see whenever it changes.
            – Smartcat
            Nov 13 at 17:48
















          Thank you. I'm fairly new to Swift, but I will look into refactoring my code to use a Singleton class.
          – burgoyne
          Nov 10 at 6:15




          Thank you. I'm fairly new to Swift, but I will look into refactoring my code to use a Singleton class.
          – burgoyne
          Nov 10 at 6:15












          So adding that to my code, it still prints out my correct coordinates, but it still wasn't working. So I printed the NEAREST_CITY_URL and it showed the coordinates as 0.0, and 0.0. Any idea why this would be?
          – burgoyne
          Nov 10 at 17:59




          So adding that to my code, it still prints out my correct coordinates, but it still wasn't working. So I printed the NEAREST_CITY_URL and it showed the coordinates as 0.0, and 0.0. Any idea why this would be?
          – burgoyne
          Nov 10 at 17:59












          Have you tried debugging it? Set a breakpoint at userLoc = locValue and see if stepping over it results in the userLoc changing. You can also add a didSet to userLoc and add a breakpoint there to see whenever it changes.
          – Smartcat
          Nov 13 at 17:48




          Have you tried debugging it? Set a breakpoint at userLoc = locValue and see if stepping over it results in the userLoc changing. You can also add a didSet to userLoc and add a breakpoint there to see whenever it changes.
          – Smartcat
          Nov 13 at 17:48


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236342%2fadd-coordinates-from-uiviewcontroller-to-constants-file%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini