Ios 如何以编程方式在mapKit中添加搜索函数
我正在尝试在我的地图中添加搜索功能,比如在apple maps应用程序中,搜索栏出现在视图的底部,或者像uber这样的应用程序,搜索栏固定在视图上——任何允许用户在我的应用程序的地图功能中搜索位置的方式 然而,我试图在swift中以编程方式完成应用程序,而不是使用故事板 我确信修复相对简单,比如mapView.addSubviewsearchBar,但我找不到正确的语法。我已经尝试了几天寻找答案,但只能找到故事板教程 当我发现如何添加搜索栏时,我猜这只是一个引用MKLocalSearchRequest中的用户搜索查询作为自然语言搜索的例子 由于我无法接近解决这个问题,因此没有示例代码。如果有人能提供将此功能添加到我的地图的示例代码,将不胜感激Ios 如何以编程方式在mapKit中添加搜索函数,ios,swift3,mapkit,mkmapview,Ios,Swift3,Mapkit,Mkmapview,我正在尝试在我的地图中添加搜索功能,比如在apple maps应用程序中,搜索栏出现在视图的底部,或者像uber这样的应用程序,搜索栏固定在视图上——任何允许用户在我的应用程序的地图功能中搜索位置的方式 然而,我试图在swift中以编程方式完成应用程序,而不是使用故事板 我确信修复相对简单,比如mapView.addSubviewsearchBar,但我找不到正确的语法。我已经尝试了几天寻找答案,但只能找到故事板教程 当我发现如何添加搜索栏时,我猜这只是一个引用MKLocalSearchRequ
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)
}
}
这就是你想做的事情吗
let mySearchBar = UISearchBar()
mySearchBar.frame = CGRect(x: 0, y: view.frame.size.height - 50, width: view.frame.size.width, height: 50)
mySearchBar.showsCancelButton = true
mySearchBar.placeholder = "sample"
self.view.addSubview(mySearchBar)
我可以看到您当前的代码吗?整个地图视图控制器在edit@kosukeogawat中看起来应该可以工作,但该栏没有显示。我也尝试过添加锚定约束,并将代码放在方法中以及直接放在viewDidLoad中,但都不起作用。您对我可能遗漏的内容或搜索条形码应插入的位置有何建议?您有何建议@Kosuke Ogawaview.frame.size.height-50是我代码中的示例。您应该更改适当的值。e、 g.视图.框架.尺寸.高度-100