2017-03-16 5 views
1

Dies ist mein erstes Mal auf Stack Overflow. Ich habe versucht, eine App zu machen, die den aktuellen Standort des Benutzers findet und die Informationen ausgibt, die die Ärzte dem Benutzer nahe bringen. Bisher kann ich den aktuellen Standort finden, ich kann jedoch keine Annotation für die Ärzte in der Nähe des aktuellen Standorts hinzufügen.Anmerkungen für MKLocalSearch in Swift 3 - Kartenansicht Aktueller Standort

Hier ist mein Code so weit:

import UIKit 
import MapKit 
import CoreLocation 

class ThirdViewController: UIViewController, CLLocationManagerDelegate { 


    @IBOutlet weak var map: MKMapView! 

    let manager = CLLocationManager() 

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) 
    { 
     let location = locations[0] 
     let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01) 

     let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude) 
     let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span) 

     map.setRegion(region, animated: true) 
     self.map.showsUserLocation = true 
    } 

    override func viewDidLoad() 
    { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

     manager.delegate = self 
     manager.desiredAccuracy = kCLLocationAccuracyBest 
     manager.requestWhenInUseAuthorization() 
     manager.startUpdatingLocation() 

     let request = MKLocalSearchRequest() 
     request.naturalLanguageQuery = "doctor" 

     request.region = map.region 

     let localSearch:MKLocalSearch = MKLocalSearch(request: request) 
     localSearch.start(completionHandler: {(result, error) in 

      for placemark in (result?.mapItems)! { 
       if(error == nil) { 

        let annotation = MKPointAnnotation() 
        annotation.coordinate = CLLocationCoordinate2DMake(placemark.placemark.coordinate.latitude, placemark.placemark.coordinate.longitude) 
        annotation.title = placemark.placemark.name 
        annotation.subtitle = placemark.placemark.title 
        self.map.addAnnotation(annotation) 

       } 
       else 
       { 
        print(error ?? 0) 
       } 
      } 
     }) 
    } 


    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


} 

Alle Antworten geschätzt werden, und wenn Sie einen Rat für das, was ich mich eine Frage stellen, beim nächsten Mal tun soll, bitte verlassen unten. Vielen Dank.

+0

Lernen, die Frage zu formatieren, wäre ein guter erster Schritt. – matt

+0

Entschuldigung, aber danke für den Rat –

Antwort

0

Ihre Kartenansicht benötigt einen Delegaten mit einer map​View(_:​view​For:​) Implementierung. Andernfalls hat das Hinzufügen einer Anmerkung keine sichtbaren Auswirkungen.

0

Um eine Annotation hinzuzufügen, müssen Sie MKMapViewDelegate erben. Fügen Sie dann Ihrer Initialisierungsmethode mapView.delegate = self hinzu. und implementieren diese Funktionen:

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { 
    if !(view.annotation! is MKUserLocation) { 
     let customPin = view.annotation as! CustomPin //if u wanna use custom pins 

     configureDetailView(annotationView: view, spotPin: customPin.spotDetailsItem) //configuring view for tap 
    } 
} 

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

    if !(annotation is CustomPin) { 
     return nil 
    } 

    let identifier = "CustomPin" 

    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) 
    if annotationView == nil { 
     annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier) 
     annotationView?.canShowCallout = true 
    } else { 
     annotationView!.annotation = annotation 
    } 

    return annotationView 
} 

configureDetailView Methode und andere Informationen, die Sie here finden (es ist ziemlich umständlich ist)

Verwandte Themen