使用Slider SWIFT4调整MKCircle的大小

问题描述 投票:0回答:2

我的目的是做与Facebook应用程序完全相同的事情。这是我目前的代码:

class MapFilterViewController: UIViewController, CLLocationManagerDelegate {

    @IBOutlet weak var mapView: MKMapView!
    @IBOutlet weak var distanceSlider: UISlider!
    @IBOutlet weak var distanceLabel: UILabel!
    @IBOutlet weak var applyButton: UIButton!

    let locationManager = CLLocationManager()


    override func viewDidLoad() {
        super.viewDidLoad()

        applyButton.layer.cornerRadius = 5

        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()

        mapView.delegate = self

    }

    func showCircle(coordinate: CLLocationCoordinate2D, radius: CLLocationDistance, mapView: MKMapView) {
        let circle = MKCircle(center: coordinate, radius: radius)
        mapView.add(circle)
    }

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

        let location = locations.last as! CLLocation

        let center =  CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
        let span = MKCoordinateSpanMake(0.05, 0.05)
        let region = MKCoordinateRegion(center : center, span : span)

        print("longitude = \(location.coordinate.longitude), latitude = \(location.coordinate.latitude)")


        mapView.setRegion(region, animated: true)
        mapView.showsUserLocation = true

        showCircle(coordinate: location.coordinate, radius: 1000, mapView: mapView)
        print(location.coordinate)

    }
    @IBAction func sliderValueChange(_ sender: Any) {
        distanceLabel.text = String(distanceSlider.value) + " km"


    }    

我不知道我应该如何在我的sliderValueChange动作中获取圆的半径并得到相同的动画

已经搜索了很多,大部分结果都在Objective-C中,我现在正在学习Swift 3周。

谢谢您的帮助

swift mapkit
2个回答
0
投票

我认为最简单的方法是删除圆圈并创建一个新圆圈(因为你不能修改cercle的半径)。

以下是您可以使用的一些代码:

var circle: MKCircle?

@IBAction func sliderValueChange(_ sender: UISlider) {
    mapView.remove(circle)
    let newRadius = sender.value * 1000
    circle = MKCircle(center: coordinate, radius: radius)
    mapView.add(circle)
}

在你学习的过程中,我认为你应该从这样的事情开始。您也可以尝试使用叠加的比例,但我认为它会更复杂,我不确定结果会好得多。


0
投票

我被迫打开圈子!调用删除功能,因为我的圈子被初始化为MKCircle?以下是您的反馈后的代码(抱歉不知道如何在代码区域中放置“class”,我是stackvoerflow的新手):

class MapFilterViewController:UIViewController,CLLocationManagerDelegate,MKMapViewDelegate {

@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var distanceSlider: UISlider!
@IBOutlet weak var distanceLabel: UILabel!
@IBOutlet weak var applyButton: UIButton!

let locationManager = CLLocationManager()
var circle : MKCircle?
var coords : CLLocationCoordinate2D?

override func viewDidLoad() {
    super.viewDidLoad()

    applyButton.layer.cornerRadius = 5

    distanceLabel.text = String(Int(distanceSlider.value)) + " km"

    locationManager.delegate = self
    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()

    mapView.delegate = self
    self.mapView.delegate = self

}

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

    let location = locations.last as! CLLocation

    let center =  CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
    let span = MKCoordinateSpanMake(0.05, 0.05)
    let region = MKCoordinateRegion(center : center, span : span)

    mapView.setRegion(region, animated: true)
    mapView.showsUserLocation = true

    circle = MKCircle(center : location.coordinate, radius: CLLocationDistance(distanceSlider.value*100))
    mapView.add(circle!)

    coords = location.coordinate

}
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
    mapView.remove(circle!)
    circle = MKCircle(center : mapView.centerCoordinate, radius: CLLocationDistance(distanceSlider.value*100))

    mapView.add(circle!)
}

@IBAction func sliderValueChange(_ sender: Any) {
    mapView.remove(circle!)
    distanceLabel.text = String(Int(distanceSlider.value)) + " km"
    let newRadius = distanceSlider.value*100
    circle = MKCircle(center : coords!, radius: CLLocationDistance(distanceSlider.value*100))

    mapView.add(circle!)
}


func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    // If you want to include other shapes, then this check is needed. If you only want circles, then remove it.
    let circleOverlay = overlay as? MKCircle
    let circleRenderer = MKCircleRenderer(overlay: circleOverlay!)
    circleRenderer.fillColor = UIColor(named :"darkRed")
    circleRenderer.alpha = 0.1

    return circleRenderer
}

}

© www.soinside.com 2019 - 2024. All rights reserved.