回到我的位置按钮Swift

时间:2017-12-10 17:45:52

标签: ios swift

我正在构建一个带有地图,我的位置和搜索按钮的应用程序,现在我需要在研究后实现一个按钮返回我的位置,我该怎么做?我不知道怎么开始...... 这是我的实际代码

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)
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

如果您希望地图显示用户位置,您应首先将MKMapView属性showsUserLocation设置为true以在地图中显示用户位置,然后将userTrackingMode属性设置为.follow

mapView.showsUserLocation = true
mapView.userTrackingMode = .follow