我的目的是做与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周。
感谢您的帮助
答案 0 :(得分:0)
我认为最简单的方法是删除圆圈并创建一个新圆圈(因为你无法修改cercle的半径)。
以下是您可以使用的一些代码:
{{1}}
在你学习的过程中,我认为你应该从这样的事情开始。您也可以尝试使用叠加的比例,但我认为它会有点复杂,我不确定结果会好得多。
答案 1 :(得分:0)
我被迫打开圈子!调用删除功能,因为我的圈子被初始化为MKCircle? 以下是您的反馈后的代码(抱歉,我不知道如何在代码区域中放置" class"我是stackvoerflow的新手):
类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
}
}