2017-04-19 138 views
1

我試圖根據labeltext查找地圖位置。 mapview不會在模擬器中更新。一旦它找到了位置,我希望它停止搜索以節省電池時間。如何解決這個基於labeltext的地圖位置

這裏是我的代碼

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

你需要讓你所做的MKLocalSearchRequest對象的請求:

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

我怎麼做在於: –

+0

你需要什麼特別的幫助?將該代碼放在您創建請求對象的位置並添加代碼以更新視圖 –