如何使用滑块更改mapkit地图视图缩放级别?

时间:2016-06-20 01:36:35

标签: ios swift mapkit

所以我有一个地图视图(基于mapkit)和我的故事板中的滑块。当用户滑动时,地图会根据其动作放大或缩小。我该如何实现?我认为这可能与longitudeDelta和latitudeDelta有关。

请帮帮我。

import UIKit
import MapKit
import CoreLocation

class ConfigureViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

    @IBOutlet weak var mapkitView: MKMapView!
    @IBOutlet weak var travelRadius: UILabel!
    @IBAction func button(sender: AnyObject) {


    }

    @IBAction func sliderChanged(sender: AnyObject) {
        let sliderValue = lrintf(sender.value)
        travelRadius.text = "\(sliderValue) mi."
        let delta = Double(self.sliderChanged(sender.value))
        var currentRegion = self.mapkitView.region

        currentRegion.span = MKCoordinateSpan(latitudeDelta: delta, longitudeDelta: delta)
        self.mapkitView.region = currentRegion

    }




    let locationManager = CLLocationManager()


    @IBOutlet weak var mapKitView: MKMapView!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.requestWhenInUseAuthorization()
        self.locationManager.startUpdatingLocation()

        self.mapKitView.showsUserLocation = true




        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {


        let location = locations.last

        let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
        let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 1.5,
            longitudeDelta: 1.5))


        self.mapKitView.setRegion(region, animated: false)

        self.locationManager.stopUpdatingLocation()
    }
    func  locationManager(manager: CLLocationManager, didFailWithError error: NSError)
    {
        print ("Errors:" + error.localizedDescription)
    }






}

Screenshot of my storyboard

1 个答案:

答案 0 :(得分:1)

您可以通过region的{​​{1}}属性设置缩放。它定义了地图的中心点和跨度(即缩放级别)

修改

帮自己一个忙,为滑块定义一个单独的mapView。事实证明,您的滑块以英里为单位进行测量,但跨度以纬度和经度为单位进行测量。 1英里纬度/经度以英里为单位的长度取决于您在地球上的位置。维基百科对latitudelongitude进行了一些讨论。 假设你在赤道上,那么两度的转换都是69英里。

(请记住连接插座)

IBOutlet