在应用运行时获取用户位置,并在使用按钮

时间:2017-09-19 15:38:48

标签: ios swift mkmapview core-location

我正在使用Swift 3开发一个关于MapKit的项目。下面的代码是一旦加载应用程序,地图将默认加载。然后,当用户按下按钮时,它会将您带到用户的位置。

我想解决这个问题的方法是我希望它在应用运行后立即加载用户的位置,然后当用户决定在屏幕上移动时,她/他可以点击按钮然后再次重新定位用户位置。就像它在地图上的工作方式一样。

when app loads capture image

when button is hit

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {
    // connecting the map from the mainBoard and refering to it as "myMap".....
    @IBOutlet weak var myMap: MKMapView!

    @IBAction func refLocation(_ sender: Any) {
        manager.startUpdatingLocation()
    }

    let manager = CLLocationManager()

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let location  = locations.last {
            let span: MKCoordinateSpan = MKCoordinateSpanMake(0.0075,0.0075)
            let myLocation :CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
            let region: MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
            myMap.setRegion(region, animated: true)
        }

        self.myMap.showsUserLocation = true

        manager.stopUpdatingLocation()
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.requestWhenInUseAuthorization()
    }
}

3 个答案:

答案 0 :(得分:0)

不清楚你在问什么。

如果您想获得单个位置更新,可以使用函数func requestLocation()

您可以在视图控制器的viewDidLoad中创建一个位置管理器(并将自己设置为其委托)并调用requestLocation()一次。

然后在按钮操作中再次拨打requestLocation()

使您的didUpdateLocations方法始终重新定位地图。这样,您只需点击一下按钮即可收到一次更新,一旦收到更新,地图就会重新显示。

答案 1 :(得分:0)

尝试 Swift 3

的此代码
import UIKit
import MapKit
import CoreLocation


class ViewController2: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

var locationManager = CLLocationManager()
var currentLocation = CLLocation()
var currentLat: Double!
var currentLon: Double!
@IBOutlet weak var mapView: MKMapView!



override func viewDidLoad() {
    super.viewDidLoad()

    myLocation()
}


func myLocation()
{
    if currentLocation.coordinate.latitude == 0.00 || currentLocation.coordinate.longitude == 0.0
    {
        print("no location")
    }
    else
    {
        self.locationManager.requestWhenInUseAuthorization()
        if CLLocationManager.locationServicesEnabled()
        {
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
        }

        let locManager = CLLocationManager()
        locManager.requestWhenInUseAuthorization()

        if(CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse ||
            CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways)
        {
            currentLocation = locManager.location!
        }

        print("currentLongitude: \(currentLocation.coordinate.longitude)")
        print("currentLatitude: \(currentLocation.coordinate.latitude)")

        currentLon = currentLocation.coordinate.longitude
        currentLat = currentLocation.coordinate.latitude

        let span = MKCoordinateSpanMake(0.2, 0.2)
        let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: currentLat, longitude: currentLon), span: span)
        mapView.setRegion(region, animated: true)
    }

}

@IBAction func MyLocationBtnPressed(_ sender: Any)
 {
    let span = MKCoordinateSpanMake(0.1, 0.1)
    let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: currentLat, longitude: currentLon), span: span)
    mapView.setRegion(region, animated: true)
 }
}

答案 2 :(得分:0)

您不必再次调用startUpdatingLocation(),因为MapView始终知道userLocation。

替换

@IBAction func refLocation(_ sender: Any) {
    manager.startUpdatingLocation()
}

@IBAction func refLocation(_ sender: Any) {
    let region: MKCoordinateRegion = MKCoordinateRegionMake(myMap.userLocation.coordinate, myMap.region.span)
    mapView.setRegion(region, animated: true)
}