MapLocation基于labeltext

时间:2017-04-19 18:37:24

标签: swift location mapkit

我正在尝试根据labeltext查找地图位置。 mapview不会在模拟器中更新。一旦找到了位置,我希望它停止搜索以节省电池时间。我该如何解决这个问题

这是我的代码

override func viewDidLoad() {
        super.viewDidLoad()

        userMap.delegate = self

    //LocationManager
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.requestLocation()
        locationManager.startUpdatingLocation()

    //Location View
        userMap.showsUserLocation = true
        userMap.setUserTrackingMode(.follow, animated: true)

        func getDirections(){
            if let selectedPin = selectedPin {
                let mapItem = MKMapItem(placemark: selectedPin)
                let launchOptions = [MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving]
                mapItem.openInMaps(launchOptions: launchOptions)
            }
        }

extension InformationTableViewController : CLLocationManagerDelegate {

        private func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
            if status == .authorizedWhenInUse {
                locationManager.requestLocation()
                let request = MKLocalSearchRequest()
                request.naturalLanguageQuery = schoolLbl.text
                request.region = userMap.region

            }
        }

        func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
            if let location = locations.first {
                let span = MKCoordinateSpanMake(0.01, 0.01)
                let region = MKCoordinateRegion(center: location.coordinate, span: span)
                userMap.setRegion(region, animated: true)
                newPin.coordinate = location.coordinate
                userMap.addAnnotation(newPin)
            }
        }

        func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
            print("error:: \(error)")
            let alert = UIAlertController(title: "Oh no....", message: "Could not find the right location..", preferredStyle: UIAlertControllerStyle.alert)

            // Add an action (button)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))

            self.present(alert, animated: true, completion: nil)
            return

        }

        func dropPinZoomIn(placemark:MKPlacemark){

            selectedPin = placemark

            userMap.removeAnnotations(userMap.annotations)
            let annotation = MKPointAnnotation()
            annotation.coordinate = placemark.coordinate
            annotation.title = placemark.name
            if let city = placemark.locality,
                let state = placemark.administrativeArea {
                annotation.subtitle = "\(city) \(state)"
            }
            userMap.addAnnotation(annotation)
            let span = MKCoordinateSpanMake(0.5, 0.5)
            let region = MKCoordinateRegionMake(placemark.coordinate, span)
            userMap.setRegion(region, animated: true)
        }

        func userMap(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?{
            if annotation is MKUserLocation {
                //return nil so map view draws "blue dot" for standard user location
                return nil
            }
            let reuseId = "pin"
            var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            pinView?.pinTintColor = UIColor.blue
            pinView!.animatesDrop = true
            pinView?.canShowCallout = true
            let smallSquare = CGSize(width: 30, height: 30)
            let button = UIButton(frame: CGRect(origin: CGPoint.zero, size: smallSquare))
            button.setBackgroundImage(UIImage(named: "briefcase"), for: .normal)
            button.addTarget(self, action: Selector(("getDirections")), for: .touchUpInside)
            pinView?.leftCalloutAccessoryView = button
            return pinView
        }
    }

1 个答案:

答案 0 :(得分:1)

您需要使用您创建的MKLocalSearchRequest对象发出请求:

let search = MKLocalSearch(request: request)
search.start { (resp: MKLocalSearchResponse?, e: Error?) in
    if let resp = resp {
        // Update your map in here
    }
}