位置管理器功能未被调用

时间:2015-09-16 11:04:43

标签: 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中使用地图工具包来获取位置但是(位置管理器)这些功能没有被调用

2 个答案:

答案 0 :(得分:1)

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

obLocatinManager = CLLocationManager()

使用模拟器时,您可以通过此菜单调用位置更改。我需要首先做一些改变,我想尝试在模拟器中捕获位置更改委托。

enter image description here

答案 1 :(得分:-1)

将obLocatinManager = CLLocationManager()放入视图中,然后将其运行到真实设备上。