ios - 位置管理器功能未被调用

标签 ios swift

import UIKit
import MapKit
import CoreLocation

class StartViewController: UIViewController, CLLocationManagerDelegate
{

var latitude,longitude: Double?
var obLocatinManager: CLLocationManager?
@IBOutlet var objMapView: MKMapView?
@IBOutlet var standard: UIButton?
@IBOutlet var satellite: UIButton?
@IBOutlet var hybrid: UIButton?
@IBOutlet var mapView: UILabel?






override func viewDidLoad() {
    super.viewDidLoad()

    edgesForExtendedLayout = UIRectEdge.None
    let swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
    swipeRight.direction = UISwipeGestureRecognizerDirection.Left
    self.view.addGestureRecognizer(swipeRight)

       }

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func loadUserLocation() -> Void
{

    obLocatinManager?.delegate = self;
    obLocatinManager?.distanceFilter = kCLDistanceFilterNone
    obLocatinManager?.desiredAccuracy = kCLLocationAccuracyHundredMeters


    obLocatinManager?.requestWhenInUseAuthorization()

    obLocatinManager?.startUpdatingLocation()
}

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {

    let newlocaton = locations[1] as! CLLocation
    self.latitude = newlocaton.coordinate.latitude
    self.longitude = newlocaton.coordinate.longitude
    obLocatinManager?.stopUpdatingLocation()
    self.loadMapView()

}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
    obLocatinManager?.stopUpdatingLocation()
}

func loadMapView() -> Void
{
    var latDelta:CLLocationDegrees = 0.2
    var longDelta:CLLocationDegrees = 0.2
    var objCoor2D: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: self.latitude!, longitude: self.longitude!)
    var objCoorSpan: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
    var region: MKCoordinateRegion = MKCoordinateRegionMake(objCoor2D, objCoorSpan)
    self.objMapView!.setRegion(region, animated: false)

}

@IBAction func didtapStandard(sender: UIButton)
{
   self.standard?.backgroundColor = UIColor.greenColor()
    self.satellite?.backgroundColor = UIColor.clearColor()
    self.hybrid?.backgroundColor = UIColor.clearColor()
    self.objMapView?.mapType = MKMapType.Standard
    self.loadUserLocation()
    println(self.longitude)
    println(self.latitude)
}
@IBAction func didtapSatellite(sender: UIButton)
{
    self.satellite?.backgroundColor = UIColor.greenColor()
    self.standard?.backgroundColor = UIColor.clearColor()
    self.hybrid?.backgroundColor = UIColor.clearColor()
    self.objMapView?.mapType = MKMapType.Satellite
    self.loadUserLocation()
    println(self.longitude)
    println(self.latitude)
}
@IBAction func didtapHybrid(sender: UIButton)
{
    self.hybrid?.backgroundColor = UIColor.greenColor()
    self.satellite?.backgroundColor = UIColor.clearColor()
    self.standard?.backgroundColor = UIColor.clearColor()
    self.objMapView?.mapType = MKMapType.Hybrid
    self.loadUserLocation()
    println(self.longitude)
    println(self.latitude)
}

我尝试使用 IOS 中的 map 套件获取位置,但(位置管理器)这些函数没有被调用

最佳答案

您永远不会在任何地方初始化 CLLocationManager 对象。将以下代码放入View Did Appear中

obLocatinManager = CLLocationManager()

当您使用模拟器时,您可以通过此菜单调用位置更改。我认为我需要首先进行一些更改,以尝试在模拟器中捕获位置更改委托(delegate)。

enter image description here

关于ios - 位置管理器功能未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32606832/

相关文章:

ios - 具有存储枚举的条件枚举开关

ios - 自定义 UITableViewCell 不工作(drawRect :) on device [Swift]

ios - 如何在 Swift 中将核心数据属性设置为 nil?

swift - iOS 10 用户通知框架。日历触发器

Swift: bool 如果为真,则不显示图像

ios - 在 Swift 中调用串行队列中的函数

ios - UITextField 没有对 UITextContentType 给出任何建议

swift - 为什么 swift 中要在这个参数前面加#

xcode - 改变场景会导致缩放

ios - 根据命令连续加载 CustomViewControllers