ios - 将搜索栏和位置按钮添加到 MapKit 视图控制器
问题描述
这可能是一个简单的解决方案,但我已经尝试了它看起来的一切。在我的登录窗口之后,我似乎无法向我的 MapKit 视图控制器添加搜索框和按钮。我以编程方式而不是从情节提要添加了我的 MapKit,并且似乎无法在其上添加任何叠加层。这是我之前尝试添加的代码,UIButton
但UITextField
它似乎没有出现,所以我已将其删除。
我试图让用户输入一个地址,他们将从当前位置获取步行路线,其中包含路线多边形叠加层以及用于查找当前位置的按钮,例如在普通地图应用程序的底角。
import MapKit
import CoreLocation
class mapViewController: UIViewController, CLLocationManagerDelegate {
let mapView = MKMapView()
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
setupMapView()
checkLocationServices()
}
func setupMapView() {
view.addSubview(mapView)
mapView.translatesAutoresizingMaskIntoConstraints = false
mapView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
mapView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
mapView.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor).isActive = true
mapView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor).isActive = true
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
let region = MKCoordinateRegion.init(center: location.coordinate, latitudinalMeters: 4000, longitudinalMeters: 4000)
mapView.setRegion(region, animated: true)
}
func checkLocationAuthorization() {
switch CLLocationManager.authorizationStatus() {
case .authorizedWhenInUse:
mapView.showsUserLocation = true
followUserLocation()
locationManager.startUpdatingLocation()
break
case .denied:
// Show alert
break
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
case .restricted:
// Show alert
break
case .authorizedAlways:
break
}
}
func checkLocationServices() {
if CLLocationManager.locationServicesEnabled() {
setupLocationManager()
checkLocationAuthorization()
} else {
// the user didn't turn it on
}
}
func followUserLocation() {
if let location = locationManager.location?.coordinate {
let region = MKCoordinateRegion.init(center: location, latitudinalMeters: 4000, longitudinalMeters: 4000)
mapView.setRegion(region, animated: true)
}
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
checkLocationAuthorization()
}
func setupLocationManager() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
}
}
编辑:我设法添加了一个 UISearchController:
let searchController: UISearchController = {
let sc = UISearchController()
sc.obscuresBackgroundDuringPresentation = false
sc.searchBar.placeholder = NSLocalizedString("Search Directions", comment: "")
sc.searchBar.barTintColor = .systemPink
return sc
}()
解决方案
推荐阅读
- concurrency - 读写器优先级的读写器问题
- python - 如何使用 Python OpenCV 在图像中获得泛滥区域及其边界?
- c# - 如何处理匿名类型的铸造委托
, 委托 T 用于 Where IEnumerable 的 () 方法 - android - Android Studio 崩溃错误 - 如何解决此错误
- verilog - Verilog - 尝试复制另一个模块的输出时出现不匹配错误
- c++ - 如何同时运行 2 种方法
- python - 音频没有播放 pygame
- html - Ionic 3 - 画布绘制 - 如何保存和加载?
- vue.js - Vue CLI | vue.config.js,如何将资产放在根目录中?
- reactjs - 为什么在反应中使用输入值