0

I have scoured the interwebs and stackoverflow, and I can't find a solution to my problem.

I am attempting to:

  1. Get a user's current location (lat & long)
  2. Calculate the distance between a user's current location and another location (lat & long) that I set internally
  3. Return the distance in a list view

So far, I can accomplish this if I manually set my current location, but I need to to update.

I have had success returning my current location (I set it as Apple headquarters in the Simulator) in the log, but no success in the actual app or simulator.

Here's what I have:

import UIKit
import CoreLocation
import MapKit

class ViewController: UITableViewController, CLLocationManagerDelegate, MKMapViewDelegate {




override func prefersStatusBarHidden() -> Bool {
    return true
}


var shops = [coffeeShop]()

var locationManager = CLLocationManager()


override func viewDidLoad() {
    super.viewDidLoad()



    // Ask for Authorisation from the User.
    self.locationManager.requestAlwaysAuthorization()

    // For use in foreground
    self.locationManager.requestWhenInUseAuthorization()

    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.startUpdatingLocation()
    }




    loadShops()

}


func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let locValue:CLLocationCoordinate2D = manager.location!.coordinate
            print("locations = \(locValue.latitude) \(locValue.longitude)")

}


func loadShops() {

    let currentLocation = CLLocation()
    let currentLat = currentLocation.coordinate.latitude
    let currentLong = currentLocation.coordinate.longitude


    var myLocation = CLLocation(latitude: currentLat, longitude: currentLong)


    let shopLocation1 = CLLocation(latitude: 39.7886939, longitude: -86.1547275)
    let distance1 = myLocation.distanceFromLocation(shopLocation1) / 1000
    let shop1 = coffeeShop(location: distance1)!

}

In addition, I have everything set in the info.plist and all of that good stuff.

HOW DO I MAKE THIS WORK!? * weeps softly *

Thanks in advance for all of your help!

scttcrry
  • 69
  • 8
  • What part isn't working? Have you got current location detection working? Comment back `@JohnRamos`. – owlswipe Jun 15 '16 at 02:04
  • You don't need to request for location permission twice. – Lumialxk Jun 15 '16 at 02:04
  • Have you only tried in the simulator? If so, did you turn on the fake location for the simulator? – Lucas Jun 15 '16 at 02:07
  • 3
    `let currentLocation = CLLocation()` doesn't get your current location. It makes a new location object with coordinates 0,0. You need to use the location you get in the delegate method `didUpdateLocations` – Paulw11 Jun 15 '16 at 03:05
  • @JohnRamos The part that's not working is in loadShops() myLocation isn't pulling my actual location. – scttcrry Jun 16 '16 at 00:27
  • @Dilts I have tried in simulator and on my phone. I have the simulator set to Apple HQs. the locationManager() prints the proper lat/long in the log, but I can't get it to calculate in loadShops() Does that make sense? – scttcrry Jun 16 '16 at 00:30
  • @Paulw11 I'm not sure I know what you mean? Would you mind elaborating or pointing me in the right direction? I'd really appreciate it!! – scttcrry Jun 16 '16 at 00:33
  • @scttcrry Try the location tutorial here: https://www.hackingwithswift.com/read/22/2/requesting-location-core-location – owlswipe Jun 16 '16 at 00:51
  • @JohnRamos I can't find anything relevant in that tutorial... I'm trying to display in a viewcontroller the distance between my current location (lat, long) and a location (lat, long) that I set internally. No beacons involved. – scttcrry Jun 16 '16 at 01:10
  • @Paulw11 I think I understand what you mean. So how do I go about that? How do I get `let currentLat` to actually equal the latitude I'm getting in the `didUpdateLocations` block? – scttcrry Jun 16 '16 at 01:26
  • @scttcrry http://rshankar.com/get-your-current-address-in-swift/ – owlswipe Jun 16 '16 at 01:50
  • @scttcrry You need to call `loadShops` from `didUpdateLocations` - you can pass it the current location – Paulw11 Jun 16 '16 at 04:05

1 Answers1

0

I was able to use the following code to achieve what I needed. Sometimes you just gotta put it out there in the universe for the universe to respond on its own. Thanks everyone for the help!

        let currentLat = self.locationManager.location!.coordinate.latitude
        let currentLong = self.locationManager.location!.coordinate.longitude
scttcrry
  • 69
  • 8