2017-06-22 57 views
0

我试图添加一个启动按钮,开始了我的导航和找到我的当前位置,但在按下我的按钮后没有任何反应? 任何帮助将不胜感激!添加启动按钮位置服务[雨燕3.0 - Xcode中]

注:地图不加载,但的LocationManager函数什么都不做,它就像它没有被按下。

继承人我的代码:

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

您已经添加了'隐私的委托方法 - 位置当在您的info.plist中使用使用说明? –

+0

是的,一切正常,直到我尝试用按钮操作它 –

回答

2

使用didUpdateLocations用于获取当前坐标 &在plist文件添加地图配置

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