我正在尝试将一个搜索功能添加到我的地图中,就像在苹果地图应用中一样,搜索栏从视图的底部出现,或者像一个应用程序,如优步,其中搜索栏固定在view - 允许用户在我的应用的地图功能中搜索位置的任何方式。
但是,我正在尝试以编程方式在swift中完成应用程序,而不是使用故事板。
我确定修复相对简单,例如mapView.addSubview(searchBar),但我找不到正确的语法。我已经尝试了几天寻找答案,但只能找到故事板教程。
当我发现如何添加搜索栏时,我猜测这只是将MKLocalSearchRequest中的用户搜索查询引用为naturalLanguageRequest的情况。
由于我无法接近解决此问题,因此没有示例代码。如果有人能提供用于将此功能添加到我的地图中的示例代码,那将非常感谢!
class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
var mapVw: MKMapView!
let locationManager = CLLocationManager()
var regionHasBeenCentered = false
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
if !regionHasBeenCentered {
let span: MKCoordinateSpan = MKCoordinateSpanMake(0.5, 0.5)
let userLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let region: MKCoordinateRegion = MKCoordinateRegionMake(userLocation, span)
mapVw.setRegion(region, animated: true)
regionHasBeenCentered = true //set region centred to true - map will not update loction constantly, which would effetively lock the location in place nad not allow scrolling, so once the user location updated the first time, it is set and the map can be moved
}
self.mapVw.showsUserLocation = true
}
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
setupMapView()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
func setupMapView() {
mapVw = MKMapView()
mapVw.delegate = self
let leftMargin:CGFloat = 0
let topMargin:CGFloat = 0
let mapWidth:CGFloat = view.frame.size.width
let mapHeight:CGFloat = view.frame.size.height
mapVw.frame = CGRect(x: leftMargin, y: topMargin, width: mapWidth, height: mapHeight)
let noLocation = self.mapVw.userLocation.coordinate
let span:MKCoordinateSpan = MKCoordinateSpanMake(0.05, 0.05)
let pinLocation = MKCoordinateRegionMake(noLocation, span)
mapVw.setRegion(pinLocation, animated: true)
print(pinLocation)
print(noLocation)
//setup long press gesture
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(self.addAnnotation(_:)))
self.mapVw.addGestureRecognizer(longPress)
view.addSubview(mapVw)
}
//add annotation
@objc func addAnnotation(_ gestureRecognizer:UIGestureRecognizer) {
if gestureRecognizer.state != UIGestureRecognizerState.began {
return
}
let touchPoint = gestureRecognizer.location(in: self.mapVw)
let newCoordinates = self.mapVw.convert(touchPoint, toCoordinateFrom: self.mapVw)
let annotation = MKPointAnnotation()
annotation.coordinate = newCoordinates
annotation.title = "Virtual Location"
annotation.subtitle = "Dropped Pin"
self.mapVw.removeAnnotations(mapVw.annotations)//remove previous pin
self.mapVw.removeAnnotation(annotation)
//create circle attributes
let cent = newCoordinates
let rad: Double = 500
let circle = MKCircle(center: cent, radius: rad)
self.mapVw.addAnnotation(annotation)
self.mapVw.removeOverlays(self.mapVw.overlays)//remove previous circle
self.mapVw.add(circle)
print(newCoordinates)
print(circle)
}
//circle overlay function
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if overlay.isKind(of: MKCircle.self){
let circleRenderer = MKCircleRenderer(overlay: overlay)
circleRenderer.fillColor = UIColor.blue.withAlphaComponent(0.05)
circleRenderer.strokeColor = UIColor.blue
circleRenderer.lineWidth = 0.5
return circleRenderer
}
self.mapVw.removeOverlays(overlay as! [MKCircle])
print(overlay)
return MKOverlayRenderer(overlay: overlay)
}
}
答案 0 :(得分:1)
这是你想做的事吗?
bin_data.get()