gpt4 book ai didi

swift - 基于labeltext的MapLocation

转载 作者:行者123 更新时间:2023-11-28 06:20:52 30 4
gpt4 key购买 nike

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

这是我的代码

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

最佳答案

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

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

关于swift - 基于labeltext的MapLocation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43503614/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com