xcode Swift 中“我的位置”按钮的实现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41951630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:45:29  来源:igfitidea点击:

Implementation of 'My Location' button in Swift

iosswiftxcodemapkitcurrentlocation

提问by DP27

I am currently stuck while attempting to figure out how to add a button on my map that will redisplay a user's current location if they stray away from it on the map. At the moment I have the code written below that displays the user's current location.

我目前在试图弄清楚如何在我的地图上添加一个按钮时被卡住,如果用户在地图上偏离它,该按钮将重新显示用户的当前位置。目前,我在下面编写了显示用户当前位置的代码。

    import UIKit
    import MapKit
    import CoreLocation

   class GameViewController: UIViewController,CLLocationManagerDelegate
   {

var lastUserLocation: MKUserLocation?





@IBOutlet weak var Map: MKMapView!

let manager = CLLocationManager()





func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = locations[0]

    let span:MKCoordinateSpan = MKCoordinateSpanMake(0.00775, 0.00775)

    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
    manager.stopUpdatingLocation()


}



override func viewDidLoad() {
    super.viewDidLoad()
    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.requestAlwaysAuthorization()
    manager.startUpdatingLocation()




}

@IBAction func refLocation(_ sender: Any) {
    print("click")
}





override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

}

}

What I am unsure of, is what code to put inside of the @IBAction function that will allow the map to re-center to the user's current location if they stray from it while looking elsewhere.

我不确定的是,在@IBAction 函数中放置什么代码,如果他们在寻找其他地方时偏离它,它将允许地图重新居中到用户的当前位置。

回答by Nirav D

For that you can call again startUpdatingLocationmethod of CLLocationManageron your Buttonaction.

为此,您可以在您的操作上再次调用startUpdatingLocation方法。CLLocationManagerButton

To get the the correct current location of user you need to access the lastobject from the locationarray in didUpdateLocationsmethod.

要获取用户的正确当前位置,您需要last从方法中的location数组访问对象didUpdateLocations

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    //Access the last object from locations to get perfect current location
    if let location = locations.last {        
        let span = MKCoordinateSpanMake(0.00775, 0.00775)        
        let myLocation = CLLocationCoordinate2DMake(location.coordinate.latitude,location.coordinate.longitude)        
        let region = MKCoordinateRegionMake(myLocation, span)
        Map.setRegion(region, animated: true)
    }               
    self.Map.showsUserLocation = true
    manager.stopUpdatingLocation()                
}

Now simply call startUpdatingLocationon your button action.

现在只需调用startUpdatingLocation您的按钮操作。

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