Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 返回我的位置按钮Swift_Ios_Swift - Fatal编程技术网

Ios 返回我的位置按钮Swift

Ios 返回我的位置按钮Swift,ios,swift,Ios,Swift,我正在构建一个带有地图、我的位置和搜索按钮的应用程序,现在我需要实现一个按钮,以便在研究后返回到我的位置,我如何才能做到这一点?我不知道如何开始。。。 这是我的实际代码 import UIKit import MapKit import CoreLocation class MapViewController: UIViewController, UISearchBarDelegate, CLLocationManagerDelegate, MKMapViewDelegate { var

我正在构建一个带有地图、我的位置和搜索按钮的应用程序,现在我需要实现一个按钮,以便在研究后返回到我的位置,我如何才能做到这一点?我不知道如何开始。。。 这是我的实际代码

import UIKit
import MapKit
import CoreLocation
class MapViewController: UIViewController, UISearchBarDelegate, CLLocationManagerDelegate, MKMapViewDelegate {
    var userPosition: CLLocationCoordinate2D!
    var managerPosition: CLLocationManager!
    @IBOutlet weak var mapView: MKMapView!
    @IBAction func searchButton(_ sender: Any) {
        let searchController = UISearchController (searchResultsController: nil)
        searchController.searchBar.delegate = self
        present(searchController, animated: true, completion: nil)
    }
    override func viewDidLoad() {
        super.viewDidLoad()            
        self.mapView.delegate = self
        self.managerPosition = CLLocationManager()
        managerPosition.delegate = self
        managerPosition.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        managerPosition.requestWhenInUseAuthorization()
        managerPosition.startUpdatingLocation()
    }
    func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
        self.userPosition = userLocation.coordinate
        print("Position updated - lat: \(userLocation.coordinate.latitude) long: \(userLocation.coordinate.longitude)")
        let span = MKCoordinateSpanMake(0.05, 0.05)
        let region = MKCoordinateRegion(center: userPosition, span: span)
        mapView.setRegion(region, animated: true)
    }
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        //ignoring user
        UIApplication.shared.beginIgnoringInteractionEvents()
        //Activity indicator
        let activityIndicator = UIActivityIndicatorView()
        activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
        activityIndicator.center = self.view.center
        activityIndicator.hidesWhenStopped = true
        activityIndicator.startAnimating()
        self.view.addSubview(activityIndicator)
        //hide search bar
        searchBar.resignFirstResponder()
        dismiss(animated: true, completion: nil)
        //create search request
        let searchRequest = MKLocalSearchRequest()
        searchRequest.naturalLanguageQuery = searchBar.text
        let activeSearch = MKLocalSearch(request: searchRequest)
        activeSearch.start { (response, error) in
            activityIndicator.stopAnimating()
            UIApplication.shared.endIgnoringInteractionEvents()
            if response == nil {
                print("ERROR")
            } else {
                //remove annotations
                let annotations = self.mapView.annotations
                self.mapView.removeAnnotations(annotations)
                //Getting data
                let latitude = response?.boundingRegion.center.latitude
                let longitude = response?.boundingRegion.center.longitude
                //create annotations
                let annotation = MKPointAnnotation()
                annotation.title = searchBar.text
                annotation.coordinate = CLLocationCoordinate2DMake(latitude!, longitude!)
                self.mapView.addAnnotation(annotation)
                //zooming annotations
                self.managerPosition.startUpdatingLocation()
                let coordinate = CLLocationCoordinate2D(latitude: latitude!, longitude: longitude!)
                let span = MKCoordinateSpanMake(0.1, 0.1)
                let region = MKCoordinateRegionMake(coordinate, span)
                self.mapView.setRegion(region, animated: true)
            }
        }
    }
}

如果希望地图显示用户位置,应首先将MKMapView属性
showsUserLocation
设置为true,以在地图中显示用户位置,然后将userTrackingMode属性设置为
。遵循以下操作:

mapView.showsUserLocation = true
mapView.userTrackingMode = .follow