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)
}
}
}