iOS & Swift: display distance from current location? - ios

I have scoured the interwebs and stackoverflow, and I can't find a solution to my problem.
I am attempting to:
Get a user's current location (lat & long)
Calculate the distance between a user's current location and another location (lat & long) that I set internally
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!

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

Related

Why is Swift/Core Location showing my lat/long location as being in San Francisco -- even tho I'm in New York?

Hey everyone — I have written some code that identifies location via CLLocation, then converts that lat/long value to a city with CLGeoCoder().
It's working in that the code runs and id's a location -- except I am in New York, and it keeps identifying me as being in San Francisco!
Can you spot any clear mistakes I'm making in the code below? Thank you!
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[locations.count - 1]
if location.horizontalAccuracy > 0 {
locationManager.stopUpdatingLocation()
locationManager.delegate = nil
let geoCoder = CLGeocoder()
geoCoder.reverseGeocodeLocation(location, completionHandler:
{
placemarks, error -> Void in
guard let placeMark = placemarks?.first else { return }
if let locationName = placeMark.location {
print(locationName)
}
if let street = placeMark.thoroughfare {
print(street)
}
if let city = placeMark.subAdministrativeArea {
print(city)
}
if let zip = placeMark.isoCountryCode {
print(zip)
}
if let country = placeMark.country {
print(country)
}
})
}
}
}
Run your app on a device (such as an actual iPhone) if you want to use location services to detect your actual location. Testing such code on a Simulator is pretty much pointless.

Why userLocation returns (-180.0,-180.0) coordinates on Mapbox?

I use Mapbox with Swift 4 and I have a problem when I want to display the user location. I don't understand why the user location is not set as it should be.
I would get the user location coordinates in the viewDidLoad() method. To do so, I have set MGLMapViewDelegate and CLLocationManagerDelegate in my ViewController declaration. Then, in my viewDidLoad() I have:
// Mapview configuration
let mapView = MGLMapView(frame: self.mapView.bounds)
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
mapView.showsUserLocation = true
mapView.setUserTrackingMode(.follow, animated: true)
mapView.delegate = self
self.mapView.addSubview(mapView)
// User location
print("User location:")
print(mapView.userLocation!.coordinate)
But I get this:
CLLocationCoordinate2D(latitude: -180.0, longitude: -180.0)
I think it is because the location is not set when the view loads, but I need to get values in viewDidLoad().
What should I do, and why the line mapView.userLocation!.coordinate doesn't work?
EDIT
In fact, I want to use MapboxDirections to display on the map a line between the user location and a fixed point. To do it, I use this code (see the first comment):
let waypoints = [
// HERE I would use the user location coordinates for my first Waypoint
Waypoint(coordinate: CLLocationCoordinate2D(latitude: 38.9131752, longitude: -77.0324047), name: "Mapbox"),
Waypoint(coordinate: CLLocationCoordinate2D(latitude: 38.8977, longitude: -77.0365), name: "White House"),
]
let options = RouteOptions(waypoints: waypoints, profileIdentifier: .automobileAvoidingTraffic)
options.includesSteps = true
_ = directions.calculate(options) { (waypoints, routes, error) in
guard error == nil else {
print("Error calculating directions: \(error!)")
return
}
if let route = routes?.first, let leg = route.legs.first {
print("Route via \(leg):")
let distanceFormatter = LengthFormatter()
let formattedDistance = distanceFormatter.string(fromMeters: route.distance)
let travelTimeFormatter = DateComponentsFormatter()
travelTimeFormatter.unitsStyle = .short
let formattedTravelTime = travelTimeFormatter.string(from: route.expectedTravelTime)
print("Distance: \(formattedDistance); ETA: \(formattedTravelTime!)")
if route.coordinateCount > 0 {
// Convert the route’s coordinates into a polyline.
var routeCoordinates = route.coordinates!
let routeLine = MGLPolyline(coordinates: &routeCoordinates, count: route.coordinateCount)
// Add the polyline to the map and fit the viewport to the polyline.
mapView.addAnnotation(routeLine)
mapView.setVisibleCoordinates(&routeCoordinates, count: route.coordinateCount, edgePadding: .zero, animated: true)
}
}
}
Larme is correct: the user's location typically isn't available yet in -viewDidLoad. Use the -mapView:didUpdateUserLocation: delegate method to be notified when the user's location becomes available and when it updates.
If you need the user’s location before a map is shown, consider running your own CLLocationManager.
-180, -180 is the kCLLocationCoordinate2DInvalid constant from Core Location. You should typically check if CLLocationCoordinate2DIsValid() before trying to display CLLocationCoordinate2D on a map.
Sergey Kargopolov has a great example of how to obtain the user location using CLLocationManager and CLLocationManagerDelegate. Here is his code:
class ViewController: UIViewController, CLLocationManagerDelegate {
var locationManager:CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
determineMyCurrentLocation()
}
func determineMyCurrentLocation() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
//locationManager.startUpdatingHeading()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation:CLLocation = locations[0] as CLLocation
// Call stopUpdatingLocation() to stop listening for location updates,
// other wise this function will be called every time when user location changes.
// manager.stopUpdatingLocation()
print("user latitude = \(userLocation.coordinate.latitude)")
print("user longitude = \(userLocation.coordinate.longitude)")
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
{
print("Error \(error)")
}
}

Core Location didEnterRegion method can not work with more than one regions

I'm working on a project about location based reminder. I have used CoreLocation's didEnterRegion method. so when I set a location for entering region, I can take a notification but whenever I want to set another location for entry with didEnterRegion method it only see first one. The method cannot be called for second location. Could you help me?
Here is my code:
import UIKit
import CoreLocation
import MapKit
class ViewController: UIViewController,CLLocationManagerDelegate {
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let latitude1: CLLocationDegrees = 78.98
let longitude1: CLLocationDegrees = 90.09
let center1: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude1, longitude1)
let radius1: CLLocationDistance = CLLocationDistance(100.0)
let identifier1: String = "Notre Dame"
let currRegion1 = CLCircularRegion(center: center1, radius: radius1, identifier: identifier1)
let latitude: CLLocationDegrees = 20.2020
let longitude: CLLocationDegrees = 2.2945
let center: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
let radius: CLLocationDistance = CLLocationDistance(100.0)
let identifier: String = "Notre Dame"
let currRegion = CLCircularRegion(center: center, radius: radius, identifier: identifier)
locationManager.distanceFilter = 10
locationManager.desiredAccuracy = kCLLocationAccuracyBest
currRegion1.notifyOnEntry = true
currRegion.notifyOnEntry = true
locationManager.delegate=self
locationManager.requestAlwaysAuthorization()
locationManager.startMonitoring(for: currRegion1)
locationManager.startMonitoring(for: currRegion)
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
if region is CLCircularRegion {
handleEvent(forRegion: region)
}
}
func handleEvent(forRegion region: CLRegion!) {
print("Geofence triggered!")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
The official document:
You must call this method once for each region you want to monitor. If
an existing region with the same identifier is already being monitored
by the app, the old region is replaced by the new one.
So make the different identifier:
//...
let identifier1: String = "Notre Dame1"
//...
let identifier: String = "Notre Dame2"
//...

iOS Simulator not updating location

The below code worked just one time and now all of the sudden it is not showing a point of interest (POI). I tried it on two different machines and am getting the same behavior. This is what has me puzzled, is it the code or my simulator settings.
I cleaned the project, made sure I had the simulator on custom. I did have the longitude and latitude printing now all of a sudden it is not printing in the console either.
import UIKit
import MapKit
import CoreLocation
class MapViewController: UIViewController, CLLocationManagerDelegate {
#IBOutlet var map: MKMapView!
var manager:CLLocationManager!
var latitude:Double = 0.0
var longitude:Double = 0.0
var location:Double = 0.0
override func viewDidLoad() {
super.viewDidLoad()
print("init")
manager = CLLocationManager()
manager.delegate = self
manager.desiredAccuracy - kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("test test test")
let userLocation:CLLocation = locations[0]
self.latitude = userLocation.coordinate.latitude
self.longitude = userLocation.coordinate.longitude
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = "Army Recruiting"
request.region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 1600, 1600)
MKLocalSearch(request: request).startWithCompletionHandler { (response, error) in
guard error == nil else { return }
guard let response = response else { return }
guard response.mapItems.count > 0 else { return }
let randomIndex = Int(arc4random_uniform(UInt32(response.mapItems.count)))
let mapItem = response.mapItems[randomIndex]
mapItem.openInMapsWithLaunchOptions(nil)
}
print("\(self.longitude) \(self.latitude)")
}
----------------------UPDATE 1--------------------
Updating location via simulator
I have noticed the below function is not running:
func locationManager(manager: CLLocationManager, didUpdateLocations
locations: [CLLocation])
---------------------Update 2--------------------
The app worked once then stopped. Not sure what is going on. Yes I added NSLocationWhenInUseUsageDescription and NSLocationAlwaysUsageDescription
Couple of issues with your code:
override func viewDidLoad() {
super.viewDidLoad()
print("init")
manager = CLLocationManager()
manager.delegate = self
manager.desiredAccuracy - kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
}
Replace with:
override func viewDidLoad() {
super.viewDidLoad()
print("init")
manager = CLLocationManager()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
}
ADDITIONALLY (for info)
You can create GPX files for different locations.
You can also simulate routes with in the GPX files - have a delve into the Apple Documentation which shows you hows to set the GPX files to your target build. https://developer.apple.com/library/ios/documentation/IDEs/Conceptual/iOS_Simulator_Guide/CustomizingYourExperienceThroughXcodeSchemes/CustomizingYourExperienceThroughXcodeSchemes.html
There is also a good tutorial here with links for creating GPX files and routes which i have used in the past and is very useful. https://blackpixel.com/writing/2013/05/simulating-locations-with-xcode.html
The simulator can have a set of hard coded locations, and you change them or disable them in the console of xcode.
It should look like this:
If you want to test real moving location you should test it on a device.

Why does this function keep looping?

func centerMapOnLocation(location: CLLocation) {
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
regionRadius * 2.0, regionRadius * 2.0)
MapOutlet.setRegion(coordinateRegion, animated: true)
}
This function above within my view controller below continues to run although I never coded any loop in, can someone help spot the incorrect logic. Here is the rest of the view controller
import UIKit
import MapKit
import CoreLocation
class ViewControllerPublic: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
let initialLocation = CLLocation(latitude: 3.632488, longitude: -117.898886)
override func viewDidLoad() {
super.viewDidLoad()
centerMapOnLocation(initialLocation)
// Ask for Authorisation from the User.
self.locationManager.requestAlwaysAuthorization()
// For use in foreground
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locValue:CLLocationCoordinate2D = manager.location!.coordinate
print("locations = \(locValue.latitude) \(locValue.longitude)")
let currentLocation = CLLocation(latitude: locValue.latitude, longitude: locValue.longitude)
centerMapOnLocation(currentLocation)
}
let regionRadius: CLLocationDistance = 2300
func centerMapOnLocation(location: CLLocation) {
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
regionRadius * 2.0, regionRadius * 2.0)
MapOutlet.setRegion(coordinateRegion, animated: true)
}
#IBOutlet weak var MapOutlet: MKMapView!
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
You never invalidate the CLLocation manager so as #Ashish Kakkad said, whenever you get even the slightest location change your function is getting called again. If you don't want this behavior, then after you get a location in didUpdateLocations you need to do locationManager.stopUpdatingLocation(). Or, if you do want your app to update the map every time the location changes, you may want to think about changing your desired location accuracy.

Resources