Hallo,
kann mir jemand helfen? Mein Map View Controller hat die Aufgabe, eine location in einem callout zu zeigen. Beim draufklicken kommt man auf die map app. Klapp auch. Jetzt ist aber auf dem callout immer noch die annotation vom Typ MKPointAnnotation überlagert. D.h. ich mochte die rote Annotation löschen, und nur den callout angezeigt haben. Wie mache ich das ohne mein Projekt zu zerschießen?
Alles anzeigen
kann mir jemand helfen? Mein Map View Controller hat die Aufgabe, eine location in einem callout zu zeigen. Beim draufklicken kommt man auf die map app. Klapp auch. Jetzt ist aber auf dem callout immer noch die annotation vom Typ MKPointAnnotation überlagert. D.h. ich mochte die rote Annotation löschen, und nur den callout angezeigt haben. Wie mache ich das ohne mein Projekt zu zerschießen?
Quellcode
- class MapViewController: UIViewController, MKMapViewDelegate
- {
- //outlet variable is used for establishing a connection with the map view in the storyboard
- @IBOutlet var mapView: MKMapView!
- // @IBOutlet var segmentedControl: UISegmentedControl!
- var spot = Spot()
- let locationManager = CLLocationManager()
- var currentPlacemark:CLPlacemark?// it is used to save the selected spot
- var currentTransportType = MKDirectionsTransportType.automobile
- var currentRoute: MKRoute?// store the current route
- override func viewDidLoad()
- {
- super.viewDidLoad()
- // self.navigationItem.backBarButtonItem?.title = " "
- navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style: .plain, target: nil, action:nil)
- // we want to show the users location
- mapView.delegate = self//important
- mapView.showsUserLocation = true
- //request for a user's authorization for lacation services
- locationManager.requestWhenInUseAuthorization()
- let status = CLLocationManager.authorizationStatus()
- if status == CLAuthorizationStatus.authorizedWhenInUse
- {
- mapView.showsUserLocation = true
- }
- let geoCoder = CLGeocoder()
- geoCoder.geocodeAddressString(spot.location,
- completionHandler:
- {
- placemarks, error in
- if let error = error
- {
- print(error)
- return
- }
- if let placemarks = placemarks
- {
- //get the first placemark
- let placemark = placemarks[0]
- // value of current Placemark
- self.currentPlacemark = placemark
- // // add annotation
- let annotation = MKPointAnnotation()// here is the issue
- annotation.title = self.spot.name
- annotation.subtitle = self.spot.type
- if let location = placemark.location
- {
- annotation.coordinate = location.coordinate
- self.mapView.addAnnotations([annotation])
- //display the annotation
- self.mapView.showAnnotations([annotation],animated:true)
- self.mapView.selectAnnotation(annotation, animated: true)
- }
- }
- })
- // save.openInMaps(launch)
- if #available(iOS 9.0, *) {
- mapView.showsCompass = true
- mapView.showsTraffic = true
- mapView.showsScale = true
- }
- }
- func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
- // guard let annotation = annotation as? Spot else {return nil}
- let identifier = "Marker"
- var view: MKMarkerAnnotationView
- if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
- as? MKMarkerAnnotationView {
- dequeuedView.annotation = annotation as! MKAnnotation
- view = dequeuedView
- } else {
- view = MKMarkerAnnotationView(annotation: annotation as! MKAnnotation, reuseIdentifier: identifier)
- view.canShowCallout = true
- view.calloutOffset = CGPoint(x:-5,y:5)
- view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
- }
- return view
- }
- func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
- print("Annotation is tapped")
- var geocoder = CLGeocoder()
- geocoder.geocodeAddressString(spot.location) {
- placemarks, error in
- var placemark = placemarks?.first
- let regionDistance:CLLocationDistance = 1000;
- let lat: CLLocationDegrees = (placemark?.location?.coordinate.latitude)!
- let lon: CLLocationDegrees = (placemark?.location?.coordinate.longitude)!
- let coordinates = CLLocationCoordinate2DMake(lat, lon)
- let regionSpan = MKCoordinateRegion(center: coordinates, latitudinalMeters: regionDistance, longitudinalMeters: regionDistance)
- let options = [MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center), MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)]
- placemark = MKPlacemark(coordinate: coordinates)
- let mapItem = MKMapItem(placemark:placemark as! MKPlacemark)
- mapItem.name = self.spot.location
- mapItem.openInMaps(launchOptions: options)
- }
- }
- override func didReceiveMemoryWarning()
- {
- super.didReceiveMemoryWarning()
- // Dispose of any resources hat can be recreated.
- }
- }