这是我第一次遇到堆栈溢出。我一直试图制作一个应用程序,找到用户的当前位置,并输出医生接近用户的信息。到目前为止,我可以找到当前位置,但是,我无法为当前位置附近的医生添加批注。
这是我目前的代码:

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


}

感谢您的回复,如果您对我下次提问时应该做什么有任何建议,请在下面留下。谢谢您。

最佳答案

地图视图需要一个带有map​View(_:​view​For:​)实现的委托。否则,添加批注将没有可见效果。

关于swift - Swift 3中的MKLocalSearch注释- map View 当前位置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42825180/

10-13 09:25