Dan.code
Dan.code

Reputation: 441

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()
}

Upvotes: 0

Views: 158

Answers (1)

Jack
Jack

Reputation: 14369

Use didUpdateLocations delegate method for getting current coordinates & add Maps configuration in plist file as

enter image description here

//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")
    }

Upvotes: 2

Related Questions