2017-03-16 69 views
1

这是我第一次使用Stack Overflow。我一直在试图制作一个应用程序,用于查找用户的当前位置并输出医生靠近用户的信息。到目前为止,我可以找到当前位置,但是,我无法为当前位置附近的医生添加注释。Swift 3中MKLocalSearch的注解 - 地图视图当前位置

这是到目前为止我的代码:

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


} 

所有响应的赞赏,如果你对我应该做的下一次我问一个问题有什么建议,请把它下面的人。谢谢。

+0

学习格式化问题将是一个良好的开端。 – matt

+0

很抱歉,谢谢你的建议 –

回答

0

您的地图视图需要具有map​View(_:​view​For:​)实现的代理。否则,添加注释将没有可见的效果。

0

要添加注释,您需要继承MKMapViewDelegate。 然后将mapView.delegate = self添加到您的初始化方法中。 而实现这个功能:

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方法等信息,您可以找到here(它是相当繁琐)