iOS APP/SWIFT #5 (4–3) iOS SDK practices :

— Display the map : using MKMapView

  1. The first is import “MapKit” and “PlaygroundSupport”

2. Get the coordinates from Apple “Maps”

3. Using MKMapView.region to set coordinate (latitude, longitude and distance),

4. Using MKPointAnnotation to set the point “Title” and “SubTitle”. The SubTitle will be displayed after click the thumbtack. The thumbtack will become bigger.

5. Using . mapType.hybrid to change map type from Standard to Hybrid (with Satellite view) and .interestFilter[.restaurant, .cafe] can display the restaurant and coffee shop on the map view only.



import MapKit
import PlaygroundSupport

/// This is a map view that displays a map of Prague.
let mapView = MKMapView (frame: CGRect(x: 0, y: 0, width: 300, height: 300))

/// This is the region that the map view displays.
mapView.region = MKCoordinateRegion(center: CLLocationCoordinate2D (latitude: 50.087506, longitude: 14.421165), latitudinalMeters: 300, longitudinalMeters: 300)
PlaygroundPage.current.liveView = mapView

mapView.mapType = .hybrid

/// This is an annotation that marks the location of Prague Square.
let pragueSquareAnnotation = MKPointAnnotation ()

/// This is the title of the annotation.
pragueSquareAnnotation.title = "布拉格老廣場"

/// This is the coordinate of the annotation.
pragueSquareAnnotation.coordinate = CLLocationCoordinate2D(latitude: 50.087506, longitude: 14.421165)

/// This is the subtitle of the annotation.
pragueSquareAnnotation.subtitle = "沒有許願池的廣場"

mapView.addAnnotation(pragueSquareAnnotation)

/// This is the point of interest filter that the map view uses.
mapView.pointOfInterestFilter = MKPointOfInterestFilter(including: [.restaurant, .cafe])

--

--

ChengWen Lee
彼得潘的 Swift iOS / Flutter App 開發教室

After worked in office for 22 years, it is time to know the real word. I would like to be a Digital Nomad (Working everywhere) and on the way now~~~