Skip to content

Latest commit

 

History

History
83 lines (64 loc) · 2.49 KB

README.md

File metadata and controls

83 lines (64 loc) · 2.49 KB

MapKitDemo

Using MapKit. In this app we search the map and animate to a region. We also customize the MKMarkerAnnoatationView.

app screenshot

1. Show the user's location

The user's location will only show on the map view if location services is authorized.

// attempt to show the user's location if location services is authorized
mapView.showsUserLocation = true

2. Configure a MKUserTrackingButton on the map

private var userTrackingButton: MKUserTrackingButton!
userTrackingButton = MKUserTrackingButton(frame: CGRect(x: 20, y: 20, width: 44, height: 44))
mapView.addSubview(userTrackingButton)
userTrackingButton.mapView = mapView

3. Center the map view at a given coordinate

mapView.setCenter(coordinate, animated: true)

4. Center the map view at a given MKCoordinateRegion

let region = MKCoordinateRegion(center: coordinate, latitudinalMeters: 1600, longitudinalMeters: 1600)
mapView.setRegion(region, animated: true)

5. Using CoreLocation to get the coordinate for a given place name

public func convertPlaceNameToCoordinate(addressString: String, completion: @escaping (Result<CLLocationCoordinate2D, Error>) -> ()) {
  // coverting an address to a coordinate
  CLGeocoder().geocodeAddressString(addressString) { (placemarks, error) in
    if let error = error {
      print("geocodeAddressString: \(error)")
      completion(.failure(error))
      return
    }
    if let firstPlacemark = placemarks?.first,
      let location = firstPlacemark.location {
      print("place name coordinate is \(location.coordinate)")
      completion(.success(location.coordinate))
    }
  }
}

5. Customize MKMarkerAnnotationView

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
  guard annotation is MKPointAnnotation else { return nil }

  let identifier = "annotationView"
  var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKMarkerAnnotationView

  if annotationView == nil {
    annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier)
    annotationView?.canShowCallout = true
    annotationView?.glyphTintColor = .systemYellow
    annotationView?.markerTintColor = .systemBlue
    //annotationView?.glyphText = "iOS 6.3"
    annotationView?.glyphImage = UIImage(named: "duck")
  } else {
    annotationView?.annotation = annotation
  }
  return annotationView
}