How do I append Altitude Integers - swift

I need to display the current Altitude in a current app I am building. However, when the Altitude is determined, the label gets displayed with xxx.xxxxxxxxxxxx
It goes super far on the other side of the decimal point. I only want it to give a at most, 2 numbers on the other side of the decimal point. How is this possible?
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations [0]
self.speedLabel.text = String(location.speed)
self.altitudeLabel.text = String(location.altitude)
CLGeocoder().reverseGeocodeLocation(location) { (placemarks, error) in
if error != nil {
print(error!)
}
That is my current code.
Like I said, I only want to display the Altitude as, "XX.XX" and not XX.XXXXXXXXX

Try this:
extension Double {
func decimal(places: Int) -> Double {
let resultString = NSString(format: "%.\(places)f" as NSString, self) as String
return Double(resultString)!
}
}
self.altitudeLabel.text = location.altitude.decimal(places: 2)

Just try:
self.altitudeLabel.text = String(format: "%.2f", location.altitude)

Related

How can I set a var value to my current latitude or longitude? Updated code provided by nikhil

I need to access these coordinate values outside of the class in order to compute certain equations, however when I try to assign myLat to UserCoordinates.userLat or UserCoordinates.userLong an error occurs.
I've tried using both structs and classes as well as global variables however all returned various errors.
public class UserCoordinates {
static let shared:UserCoordinates = UserCoordinates()
var userLat: Double?
var userLong: Double?
func setUserLat(userLat: Double, userLong: Double) {
self.userLong = userLong
self.userLat = userLat
}
private init() {
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
guard let location = locations.last else {
return
}
userLat = Double(location.coordinate.latitude)
userLong = Double(location.coordinate.longitude)
}
}
sin(my latitude)
I want to take the sin of my latitude wherever I am how would I assign the value of userLat in the class/function to this sin calculation?
You cannot directly access a variable using a class unless it's static. If you want something that to be one instance then you should use singleton check this below code.
public class UserCoordinates {
static let shared:UserCoordinates = UserCoordinates()
var userLat: Double?
var userLong: Double?
func setUserLat(userLat: Double, userLong: Double) {
self.userLong = userLong
self.userLat = userLat
}
private init() {
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
guard let location = locations.last else {
return
}
userLat = Double(location.coordinate.latitude)
userLong = Double(location.coordinate.longitude)
}
}
UserCoordinates.shared.setUserLat(userLat: 10.0, userLong: 10.0)
let myLat = UserCoordinates.shared.userLat

Swift 4 MapKit check if mark is near to my location

I want to check when my position is changing if my location is near the Mark. I have marks with unique position. If i near for example 10m from mark i want to display alert. How i can do that?
Get visible annotations.
Calculate distance between your position and the mark.
e.g.
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let myLocation = locations.first else { return }
let annotationSet = mapView.annotations(in: mapView.visibleMapRect)
for annotation in annotationSet {
guard let annotation = annotation as? MKPointAnnotation else { continue }
let loc = CLLocation(latitude: annotation.coordinate.latitude,
longitude: annotation.coordinate.longitude)
let distance = myLocation.distance(from: loc)
if distance < 10.0 { Show Alert }
}
}
}
You can implement this delegate from CLLocationManager:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let myLoc:CLLocationCoordinate2D = manager.location!.coordinate
let distance : CLLocationDistance = mark1.distanceFromLocation(myLoc)
if distance < 10.0 { //Show Alert }
}

Adding Start Button for Location Services [Swift 3.0 - Xcode]

I am trying to add a start button to start up my navigation and to find my current location, but nothing happens after my button is pressed?
Any help would be greatly appreciated!
Note: Map does load up, but the locationManager function does nothing, its just like it hasn't been pressed.
Heres my code:
import UIKit
import MapKit
import CoreLocation
class ThirdViewController: UIViewController , CLLocationManagerDelegate{
let manager = CLLocationManager()
func START(){
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01,0.01) //shows the size of map screen
let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude,location.coordinate.longitude)
let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
map.setRegion(region, animated: true)
self.map.showsUserLocation = true
}
}
#IBAction func STARTNAV(_ sender: UIButton) {
START()
}
Use didUpdateLocations delegate method for getting current coordinates
& add Maps configuration in plist file as
//MARK: locations ...
let locationManager = CLLocationManager()
func start() {
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = 100.0;
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
//locationManager.requestWhenInUseAuthorization()
let authorizationStatus = CLLocationManager.authorizationStatus()
let selector = #selector(self.locationManager.requestWhenInUseAuthorization)
if self.locationManager.responds(to:selector) {
if authorizationStatus == .authorizedAlways
|| authorizationStatus == .authorizedWhenInUse {
self.locationManager.startUpdatingLocation()
}else{
self.locationManager.requestWhenInUseAuthorization()
}
}else{
self.locationManager.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print((locationManager.location?.coordinate.latitude) ?? "No values")
let status = CLAuthorizationStatus.self
print("status-------->\(status)")
let locationValue : CLLocationCoordinate2D = (manager.location?.coordinate)!
let location = CLLocation(latitude: locationValue.latitude, longitude: locationValue.longitude)
CLGeocoder().reverseGeocodeLocation(location, completionHandler: {(placemarks, error) -> Void in
if error != nil{
print("Reverse geocoder failed with error" + error!.localizedDescription)
return
}
if placemarks!.count > 0 {
let pm = placemarks![0]
if let locationName = pm.addressDictionary!["SubLocality"] as? NSString {
print("locationName is \(locationName)")
}
}
else{
print("Problem with the data received from geocoder")
}
})
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("locationManager-failed")
}

Completion in Updating Location Manager

I'm having an issue with the Update Location Manager.
So this is what i want to do. When the user goes into the view, it asks for location permision, when the user refuses i have set up a code that i will not post here cause it works. But if he accepts i run a function that i get the latitude and longitude of his. And then i want to run a function in which i do a query to an API. The following code is what I made
var tolat: Double = 0.0
var tolon: Double = 0.0
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
getData() { result in
switch result {
case .Success: self.eventsTable.reloadData()
case .Failure(let error): break
}
}
}
func getData(completion: Resulty<Void> -> Void) {
print("the lat \(tolat) and the lon \(tolon)")
completion(.Success())
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locValue:CLLocationCoordinate2D = manager.location!.coordinate
tolat = locValue.latitude
tolon = locValue.longitude
locationManager.stopUpdatingLocation()
}
and after the end of the view
enum Resulty<T> {
case Success(T)
case Failure(NSError)
}
So it prints 0.0 the latitude and longitude. How can i make the func getData run when the Location Latitude and Longitude actually has data???

How to store users location on CloudKit in swift?

I'm trying to store a users journey and upload it to CloudKit.
The following takes the users location whenever they move more than 5 meters and uploads as a string but I want it to be uploaded as a location list or similar so it can be pulled down later.
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let location = locations.last
let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
addCrumbPoint(center)
let message = "{\"lat\":\(location!.coordinate.latitude),\"lng\":\(location!.coordinate.longitude), \"alt\": \(location!.altitude)}"
let newSweet = CKRecord(recordType: "Sweet")
newSweet["content"] = message
let publicData = CKContainer.defaultContainer().publicCloudDatabase
publicData.saveRecord(newSweet, completionHandler: { (record:CKRecord?, error:NSError?) -> Void in
if error == nil {
print("woo")
}else{
print(error)
}
})
}
The documentation on using Location and CloudKit is written in Objective-C so any help would be brilliant.
CloudKit
CLLocationManager
You create a CKRecordID and a CKRecord. Then you set the last retrieved CLLocation on the CKRecord.
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last!
let id = CKRecordID(recordName: "01")
let locationRecord = CKRecord(recordType: "location", recordID: id)
locationRecord.setObject(location, forKey: "location")
// or locationRecord["location"] = location
let publicData = CKContainer.defaultContainer().publicCloudDatabase
publicData.saveRecord(locationRecord) { record, error in
//
}
}