MapKit - Slider zum Zoomen und Kreis vergrößern

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • MapKit - Slider zum Zoomen und Kreis vergrößern

    Hallo,

    ich habe vor wenigen Tagen/Wochen mit der Swift Programmierung für iPhones und iPads begonnen und bin dabei auch direkt auf ein Problem gestoßen.

    Ich habe eine Karte und einen Slider auf einer View platziert. Nun zeichne ich mit Hilfe des Sliders einen MKCircle/MKCircleRenderer auf der Karte. Soweit so gut. Nun würde ich gern das Zoomlevel automatisch an die Größe des Kreises anpassen und scheitere daran kläglich. All meine Versuche endeten mit viel zu weit heraus/hereingezoomten karten oder gar abstürzen der App.

    Ich hoffe, dass jemand einen tipp für mich hat. Die Google-Karten würde ich ungern verwenden, dort scheint dies ja ziemlich einfach möglich zu sein.

    Vielen Dank für eure Hilfe!
  • Aktuell sieht es so aus, allerdings ohne Zoom. Das hatte ich wieder gelöscht, da es wie gesagt nicht richtig funktionierte.
    Ich finde das Beispiel gerade nicht mehr woher ich die Zoom-Lösung hatte.

    Gedacht habe ich mir das ganze so ähnlich wie bei der eBay-Kleinanzeigen App wenn man dort oben rechts auf Ort klickt, kann man PLZ + Ort angeben und dann über den Slider einen 500km Radius eingeben.

    Quellcode

    1. //
    2. // SearchViewController.swift
    3. //
    4. // Created by Patrik on 24.02.18.
    5. // Copyright © 2018 Patrik. All rights reserved.
    6. //
    7. import UIKit
    8. import MapKit
    9. import Firebase
    10. import SwiftIconFont
    11. class SearchViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
    12. @IBOutlet weak var tabBarSearch: UITabBarItem!
    13. @IBOutlet weak var sliderValue: UISlider!
    14. @IBAction func sliderAction(_ sender: Any) {
    15. let circle = MKCircle(center: userLocation, radius: CLLocationDistance(sliderValue.value))
    16. mapView.removeOverlays(mapView.overlays)
    17. mapView.add(circle);
    18. let annotationPoint = MKMapPointForCoordinate(circle.coordinate)
    19. let zoomRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 1000, 1000)
    20. mapView.setVisibleMapRect(zoomRect, animated: true)
    21. }
    22. @IBOutlet weak var mapView: MKMapView!
    23. var locationManager: CLLocationManager!
    24. var userLocation = CLLocationCoordinate2D()
    25. override func viewDidLoad() {
    26. super.viewDidLoad()
    27. mapView.delegate = self
    28. if (CLLocationManager.locationServicesEnabled())
    29. {
    30. locationManager = CLLocationManager()
    31. locationManager.delegate = self
    32. locationManager.desiredAccuracy = kCLLocationAccuracyBest
    33. locationManager.requestAlwaysAuthorization()
    34. locationManager.startUpdatingLocation()
    35. }
    36. sliderAction(self)
    37. }
    38. /*
    39. let userValue: [String: Any] = ["email":Auth.auth().currentUser?.email, "lat":userLocation.latitude, "lon":userLocation.longitude]
    40. Database.database().reference().child("user").childByAutoId().setValue(userValue)
    41. */
    42. func kilometresToLatitudeDegrees(kms: Double) -> Double {
    43. let earthRadius = 6371.0 // in kms
    44. let radiansToDegrees = 180.0/(Double.pi)
    45. return (kms/earthRadius) * radiansToDegrees
    46. }
    47. func kilometresToLongitudeDegrees(kms: Double, atLatitude: Double) -> Double {
    48. let earthRadius = 6371.0 // in kms
    49. let degreesToRadians = (Double.pi)/180.0
    50. let radiansToDegrees = 180.0/(Double.pi)
    51. // derive the earth's radius at that point in latitude
    52. let radiusAtLatitude = earthRadius * cos(atLatitude * degreesToRadians)
    53. return (kms / radiusAtLatitude) * radiansToDegrees
    54. }
    55. func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    56. let userLocation:CLLocation = locations[0] as CLLocation
    57. locationManager.stopUpdatingLocation()
    58. let location = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)
    59. self.userLocation = location
    60. let span = MKCoordinateSpanMake(0.08, 0.08)
    61. let region = MKCoordinateRegion (center: location,span: span)
    62. mapView.setRegion(region, animated: true)
    63. }
    64. func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    65. if let overlay = overlay as? MKCircle {
    66. guard let circelOverLay = overlay as? MKCircle else {return MKOverlayRenderer()}
    67. let circleRenderer = MKCircleRenderer(circle: circelOverLay)
    68. circleRenderer.strokeColor = .blue
    69. circleRenderer.fillColor = .blue
    70. circleRenderer.alpha = 0.06
    71. return circleRenderer
    72. }
    73. return MKOverlayRenderer(overlay: overlay)
    74. }
    75. override func didReceiveMemoryWarning() {
    76. super.didReceiveMemoryWarning()
    77. }
    78. }
    Alles anzeigen



    mit folgender Methode klappt es nun ganz gut.
    Allerdings weiß ich aktuell nicht, wie ich den Sliderkonfigurieren muss damit ich beispielsweise einen Radius von 1-500KM einstellen kann. Aktuell ist der Slider wie folgt eingestellt: Value 10, Minimum 10, Maximum 1000


    Quellcode

    1. @IBAction func sliderAction(_ sender: Any) {
    2. let circle = MKCircle(center: userLocation, radius: CLLocationDistance(sliderValue.value))
    3. mapView.removeOverlays(mapView.overlays)
    4. mapView.add(circle);
    5. let location = CLLocationCoordinate2D(latitude: self.userLocation.latitude, longitude: self.userLocation.longitude)
    6. self.userLocation = location
    7. let spanValue:Double = (Double(sliderValue.value/44000.0))
    8. let span = MKCoordinateSpanMake(spanValue, spanValue)
    9. let region = MKCoordinateRegion (center: location,span: span)
    10. mapView.setRegion(region, animated: true)
    11. }
    Alles anzeigen

    Habe nun eine Ziemlich "gute" Lösung. Es funktioniert erst einmal alles. Der Slider geht nun von 0-500

    Jetzt muss ich nur noch mal gucken wie ich dem Slider "schritte" beibringen kann. Beispielsweise 0, ,5 10, 25, 50, 100, 150, 200, 250, 300, .... 500


    Quellcode

    1. //
    2. // SearchViewController.swift
    3. // TellEm
    4. //
    5. // Created by Patrik on 24.02.18.
    6. // Copyright © 2018 Patrik. All rights reserved.
    7. //
    8. import UIKit
    9. import MapKit
    10. import Firebase
    11. import SwiftIconFont
    12. class SearchViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
    13. @IBOutlet weak var kmAngabe: UILabel!
    14. var locationManager: CLLocationManager!
    15. var userLocation = CLLocationCoordinate2D()
    16. @IBOutlet weak var mapView: MKMapView!
    17. @IBOutlet weak var tabBarSearch: UITabBarItem!
    18. @IBOutlet weak var sliderValue: UISlider!
    19. @IBAction func sliderAction(_ sender: Any) {
    20. let radius:Double = Double(sliderValue.value*1000)
    21. let circle = MKCircle(center: userLocation, radius: CLLocationDistance(radius))
    22. mapView.removeOverlays(mapView.overlays)
    23. mapView.add(circle);
    24. let location = CLLocationCoordinate2D(latitude: self.userLocation.latitude, longitude: self.userLocation.longitude)
    25. self.userLocation = location
    26. let spanValue:Double = (Double(sliderValue.value/45.0))
    27. let span = MKCoordinateSpanMake(spanValue, spanValue)
    28. let region = MKCoordinateRegion (center: location,span: span)
    29. mapView.setRegion(region, animated: true)
    30. let radiusMeter:Int = Int(radius)
    31. let radiusKm:Int = Int(radius/1000)
    32. kmAngabe.text = "\(radiusMeter) Radius meter \(radiusKm) Radius km"
    33. }
    34. override func viewDidLoad() {
    35. super.viewDidLoad()
    36. mapView.delegate = self
    37. if (CLLocationManager.locationServicesEnabled())
    38. {
    39. locationManager = CLLocationManager()
    40. locationManager.delegate = self
    41. locationManager.desiredAccuracy = kCLLocationAccuracyBest
    42. locationManager.requestAlwaysAuthorization()
    43. locationManager.startUpdatingLocation()
    44. }
    45. sliderAction(self)
    46. }
    47. /*
    48. let userValue: [String: Any] = ["email":Auth.auth().currentUser?.email, "lat":userLocation.latitude, "lon":userLocation.longitude]
    49. Database.database().reference().child("user").childByAutoId().setValue(userValue)
    50. */
    51. func kilometresToLatitudeDegrees(kms: Double) -> Double {
    52. let earthRadius = 6371.0 // in kms
    53. let radiansToDegrees = 180.0/(Double.pi)
    54. return (kms/earthRadius) * radiansToDegrees
    55. }
    56. func kilometresToLongitudeDegrees(kms: Double, atLatitude: Double) -> Double {
    57. let earthRadius = 6371.0 // in kms
    58. let degreesToRadians = (Double.pi)/180.0
    59. let radiansToDegrees = 180.0/(Double.pi)
    60. // derive the earth's radius at that point in latitude
    61. let radiusAtLatitude = earthRadius * cos(atLatitude * degreesToRadians)
    62. return (kms / radiusAtLatitude) * radiansToDegrees
    63. }
    64. func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    65. let userLocation:CLLocation = locations[0] as CLLocation
    66. locationManager.stopUpdatingLocation()
    67. let location = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)
    68. self.userLocation = location
    69. let span = MKCoordinateSpanMake(0.0, 0.0)
    70. let region = MKCoordinateRegion (center: location,span: span)
    71. mapView.setRegion(region, animated: true)
    72. }
    73. func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    74. if let overlay = overlay as? MKCircle {
    75. guard let circelOverLay = overlay as? MKCircle else {return MKOverlayRenderer()}
    76. let circleRenderer = MKCircleRenderer(circle: circelOverLay)
    77. circleRenderer.strokeColor = .blue
    78. circleRenderer.fillColor = .blue
    79. circleRenderer.alpha = 0.06
    80. return circleRenderer
    81. }
    82. return MKOverlayRenderer(overlay: overlay)
    83. }
    84. override func didReceiveMemoryWarning() {
    85. super.didReceiveMemoryWarning()
    86. }
    87. }
    Alles anzeigen

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von Patrik_90 ()