TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Day 60 challenge Project 16

Forums > 100 Days of Swift

Hi, i tried challenge for project 16 but seem to be lost. Basically we need to show an array of maptypes in alertcontroller and then have the user show the map accordingly. This is part 2 of the challenge: ie Add a UIAlertController that lets users specify how they want to view the map. There's a mapType property that draws the maps in different ways. For example, .satellite gives a satellite view of the terrain.

The code stops working when i add mine. Running otherwise until milestone 1. Working code:

  func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
        guard let capital = view.annotation as? Capital else { return }
        let placeName = capital.title
        let placeInfo = capital.info

        let ac = UIAlertController(title: placeName, message: placeInfo, preferredStyle: .alert)
        ac.addAction(UIAlertAction(title: "OK", style: .default))
        present(ac, animated: true)
        //step 1 list an array of types of maps
        //step 2 show the list in UIAlertAction as separate buttons which are clickable.
        //step 3 , when pressed, call a closure that will change mapView mapType to the selected item.

//        let arrayOfMayTypes = ["standard" : standard,
//                               "hybrid" : hybrid, "satellite" : satellite,
//                               "satelliteFlyover": satelliteFlyover]
        let arrayOfMayTypesText = ["standard" ,"hybrid", "satellite", "satelliteFlyover"]
        // standard / hybrid / satellite / satelliteFlyOver
        let ac2 = UIAlertController(title: "Terrain", message: "Choose the type of terrain", preferredStyle: .alert)
        for type in arrayOfMayTypesText {
            ac2.addAction(UIAlertAction(title: type, style: .default) { [weak  mapView, type] _ in
                switch(type) {
                case "standard":
                    mapView?.mapType = MKMapType.standard
                    break
                case "hybrid":
                    mapView?.mapType = MKMapType.hybrid
                    break
                case "satellite":
                    mapView?.mapType = MKMapType.satellite
                    break
                case "satelliteFlyover":
                    mapView?.mapType = MKMapType.satelliteFlyover
                    break
                default:
                    mapView?.mapType = MKMapType.standard

                }

            })
            present(ac2, animated: true)

        }
    }
}

Not working code:

Code below:

    func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
        guard let capital = view.annotation as? Capital else { return }
        let placeName = capital.title
        let placeInfo = capital.info

        let ac = UIAlertController(title: placeName, message: placeInfo, preferredStyle: .alert)
        ac.addAction(UIAlertAction(title: "OK", style: .default))
        present(ac, animated: true)
        //step 1 list an array of types of maps
        //step 2 show the list in UIAlertAction as separate buttons which are clickable.
        //step 3 , when pressed, call a closure that will change mapView mapType to the selected item.

//        let arrayOfMayTypes = ["standard" : standard,
//                               "hybrid" : hybrid, "satellite" : satellite,
//                               "satelliteFlyover": satelliteFlyover]
        let arrayOfMayTypesText = ["standard" ,"hybrid", "satellite", "satelliteFlyover"]
        // standard / hybrid / satellite / satelliteFlyOver
        let ac2 = UIAlertController(title: "Terrain", message: "Choose the type of terrain", preferredStyle: .alert)
        for type in arrayOfMayTypesText {
            ac2.addAction(UIAlertAction(title: type, style: .default) { [weak  mapView, arrayOfMayTypesText] _ in
                mapView?.mapType = "." + type

            })
            present(ac2, animated: true)

        }
    }
}

3      

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.