Problem mit Segmented Control und MapTypes (Google Maps)

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

  • Problem mit Segmented Control und MapTypes (Google Maps)

    Hallo Leute

    Ich habe hier ein merkwürdiges Problem mit Segmented Controls:

    Die App zeigt grundsätzlich eine Map (Google Maps), bei welcher ich den MapType über eben diese Segmented Control steuern will. Hier mal mein Code:

    Quellcode

    1. @IBAction func segmentedControlAction(sender: UISegmentedControl!) {
    2. switch (sender.selectedSegmentIndex) {
    3. case 0:
    4. mapView.mapType = .normal
    5. case 1:
    6. mapView.mapType = .satellite
    7. case 2:
    8. mapView.mapType = .hybrid
    9. case 3:
    10. do {
    11. // Set the map style by passing the URL of the local file.
    12. if let styleURL = Bundle.main.url(forResource: "night", withExtension: "json") {
    13. mapView.mapStyle = try GMSMapStyle(contentsOfFileURL: styleURL)
    14. } else {
    15. NSLog("Unable to find night.json")
    16. }
    17. } catch {
    18. NSLog("One or more of the map styles failed to load. \(error)")
    19. }
    20. default:
    21. mapView.mapType = .hybrid
    22. }
    23. }
    Alles anzeigen


    Wenn ich die App nun starte, dann sehe ich die Seg Control oben wie im Interface Builder angelegt, und wenn ich "Normal", also Case 0 antippe, dann bekomme ich auch eben diese Ansicht. Aber sobald ich Case 3, also "Night" anwähle, dann wird mir zwar auch der Nachtmodus angezeigt, aber die Kontrollen geraten völlig durcheinander. Sprich, wenn ich nun erneut "Normal" Antippe bekomme ich ebenfalls den Nachtmodus zu sehen, genauso wie wenn ich "Night" antippe.

    Satellite und Hybrid funtkionieren derweil tadellos.

    Kann mir da vielleicht jemand auf die Sprünge helfen, was da in meinem Code falsch läuft?

    Beste Grüsse,

    Thaddäus
    MacBook Pro 16" M2 Max 32 GB RAM, - iPad Pro 12.9" M1 256 GB WiFi+Cellular - iPhone 14 Pro 256 GB - Apple Watch Ultra
  • Na ja, wenn Du das Segmented Control auf den 4. Eintrag (Night Mode) stellst, dann verstellst Du im MapView den mapStyle. Bei den Einträgen 1-3 wird allerdings nur der mapType verstellt und der ggf. zuvor geänderte mapStyle nicht zurückgesetzt. ;)

    Für mich scheint es so, als könne man den "Night Modus" bei allen mapTypes verwenden. Ein weiterer Eintrag im Segmented Control würde für mich daher keinen Sinn machen. Eine Checkbox bzw. ein ToggleButton scheint mir da sinnvoller.
  • Also meinst du im Prinzip, dass ich nur die Einträge 1 - 3 über die Segmented Control nehmen. sollte, und den Nightmode sozusagen Standalone über einen Toggle Switch an und ausstellen soll. Sozusagen ein Overlay über die MapTypes?
    MacBook Pro 16" M2 Max 32 GB RAM, - iPad Pro 12.9" M1 256 GB WiFi+Cellular - iPhone 14 Pro 256 GB - Apple Watch Ultra
  • Hm, daran kann es eigentlich nicht liegen weil:

    Default ist Hybrid und wird auch so gestartet. Klicke ich nun auf "Normal" kommt auch die normale Ansicht. Nun auf Night, und der Nachtmodus bzw. der im JSON definierte Style erscheint. Erneut auf "Normal" bleibt der Nachtmodus aktiv. Wenn ich nun allerdings Satellit oder Hybrid anwähle, dann kommen diese beiden Modi wieder anstandslos zum Vorschein.

    Ich erinnere mich auch, dass ich das damals unter Objective-C auch so gelöst habe, nur existiert der Code leider nicht mehr...
    MacBook Pro 16" M2 Max 32 GB RAM, - iPad Pro 12.9" M1 256 GB WiFi+Cellular - iPhone 14 Pro 256 GB - Apple Watch Ultra
  • So, ich konnte das Problem lösen:


    Quellcode

    1. // Paste the JSON string to use.
    2. private let kMapStyle = "all"

    Quellcode

    1. override func viewDidLoad() {
    2. super.viewDidLoad()
    3. //Set up map.
    4. let mainBundle = Bundle.main
    5. let styleUrl: URL? = mainBundle.url(forResource: "night", withExtension: "json")
    6. // Set the map style by passing the URL for style.json.
    7. let style = try? GMSMapStyle(contentsOfFileURL: styleUrl!)
    8. if !(style != nil) {
    9. print("The style definition could not be loaded")
    10. }
    11. mapView.isMyLocationEnabled = false
    12. mapView.delegate = self
    13. mapView.settings.rotateGestures = false
    14. mapView.settings.tiltGestures = false
    15. mapView.mapType = .hybrid
    16. mapView.mapStyle = style
    Alles anzeigen



    Und dann die Segmented Control ganz normal eingerichtet:


    Quellcode

    1. // Function for the segmented controlled map type selector
    2. @IBAction func segmentedControlAction(sender: UISegmentedControl!) {
    3. switch (sender.selectedSegmentIndex) {
    4. case 0:
    5. mapView.mapType = .normal
    6. case 1:
    7. mapView.mapType = .satellite
    8. case 2:
    9. mapView.mapType = .hybrid
    10. case 3:
    11. mapView.mapType = .terrain
    12. default:
    13. mapView.mapType = .hybrid
    14. }
    15. }
    Alles anzeigen
    Evtl. hilft es ja mal jemandem... :)
    MacBook Pro 16" M2 Max 32 GB RAM, - iPad Pro 12.9" M1 256 GB WiFi+Cellular - iPhone 14 Pro 256 GB - Apple Watch Ultra

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von Thaddäus ()

  • Den Teil hier könntest du noch etwas schicker gestaltet

    Quellcode

    1. let styleUrl: URL? = mainBundle.url(forResource: "night", withExtension: "json")
    2. // Set the map style by passing the URL for style.json.
    3. let style = try? GMSMapStyle(contentsOfFileURL: styleUrl!)
    4. if !(style != nil) {
    5. print("The style definition could not be loaded")
    6. }


    z.B. so:


    PHP-Quellcode

    1. guard
    2. let styleUrl = mainBundle.url(forResource: "night", withExtension: "json"),
    3. let style = try? GMSMapStyle(contentsOfFileURL: styleUrl)
    4. else {
    5. print("The style definition could not be loaded")
    6. return
    7. }