iOS MKMAPVIEW中心在用户位置上,然后允许在Swift 3.0中进行无限制滚动



我不相信Swift 3.0 - 三个目标:

  1. viewDidLoad上,地图以某个可以设置的缩放级别中心到用户位置(例如let span: MKCoordinateSpan = MKCoordinateSpanMake(40.0, 40.0)

  2. 一旦地图加载并以用户位置为中心,用户可以将地图移动并滚动到任何其他位置,而没有地图自动将其扣回原始用户位置

  3. 允许用户仅缩放到一定级别,但允许用户完全缩放以查看整个全局地图(在缩放级别上无限制)

这是到目前为止我的代码:

import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
    @IBOutlet weak var mapView: MKMapView!
    let locationManager = CLLocationManager()
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let location = locations[0]
        let span: MKCoordinateSpan = MKCoordinateSpanMake(40.0, 40.0)
        let userLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
        let region: MKCoordinateRegion = MKCoordinateRegionMake(userLocation, span)
        mapView.setRegion(region, animated: true)
        self.mapView.showsUserLocation = true   
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
    }
}

1。应该使用您现在拥有的代码。

2。添加检查以获取后续位置更新

didUpdateLocations方法中,添加一个Bool以检查该区域是否已经集中在用户上。

var regionHasBeenCentered = false
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = locations[0]
    if !regionHasBeenCentered {
        let span: MKCoordinateSpan = MKCoordinateSpanMake(40.0, 40.0)
        let userLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
        let region: MKCoordinateRegion = MKCoordinateRegionMake(userLocation, span)
        mapView.setRegion(region, animated: true)
        regionHasBeenCentered = true
    }
    self.mapView.showsUserLocation = true   
}

现在,在第一次更新后,地图将不再居中用户,直到将regionHasBeenCentered更改为false。这将允许用户自由滚动和缩放。

3。实施MKMapViewDelegate方法以检测地图区域更改

在视图控制器上实现MKMapViewDelegate,以便您可以检查区域更改。

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

…并将视图控制器设置为delegate

override func viewDidLoad() {
    // other things…
    mapView.delegate = self
}

然后实现以下方法,该方法将在区域更改之前立即调用。在这里,您可以检查跨度的尺寸是否太小,并将其设置为最低。

func mapView(_ mapView: MKMapView, regionWillChangeAnimated animated: Bool) {
    if mapView.region.span.latitudeDelta <= 40 && mapView.region.span.longitudeDelta <= 40 {
         let minimumSpan = MKCoordinateSpan(latitudeDelta: 40, longitudeDelta: 40)
         let minimumRegion= MKCoordinateRegion(center: mapView.centerCoordinate, span: minimumSpan)
         mapView.setRegion(minimumRegion, animated: false)
    }
}

重要说明:MKCoordinateSpan文档中,longitudeDelta将在您朝赤道移动/远离赤道时会发生变化。

longitudedelta

向东距离(以程度测量)以显示地图区域的量。经度范围跨越的公里数因当前纬度而变化。例如,一个程度的经度跨越赤道的约111公里(69英里),但在两极的距离内收缩至0公里。

此外,MKCoordinateSpan的尺寸以度度测量,40度相当多,因此您可能想更改这些值,否则用户将无法完全缩放。

最新更新