以编程方式(在Swift中)将UINavigationController嵌入MapKit中,以容纳UISearchController

时间:2018-07-15 09:42:48

标签: ios swift3 uinavigationcontroller mapkit uisearchcontroller

我正在尝试以编程方式向我的地图添加搜索栏,并且已经确定我需要向地图视图添加导航控制器,以便在视图顶部正确设置搜索栏的格式。我得出的结论是,我实际上需要一个UISearchController而不是一个UI SearchBar。但是,我没有将导航控制器从AppDelegate文件编码到应用程序中,因为我还使用了在应用程序委托中编码的tabBarController。

基本上,我正在寻找一种将UINavigationController以编程方式嵌入到我的地图视图中的方法,以便可以将UISearchController添加到NavigationController。

最终目标是创建一个具有搜索功能的地图,以允许用户搜索位置-如果有人对我如何在我已经拥有的框架内实现此目标有其他建议,将不胜感激!

我的AppDelegate和地图视图代码在下面。

AppDelegate
import UIKit
import Firebase

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    window = UIWindow(frame: UIScreen.main.bounds)
    window?.makeKeyAndVisible()

    window?.rootViewController = TabBarController()

    return true

}
}

MapViewController

import UIKit
import MapKit
import CoreLocation

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 
    }

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



@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 //adjust radius to make circle bigger.
    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)
}

}

TabBarController

import UIKit

class TabBarController: UITabBarController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    let messagesController = MessagesController()
    let messagesNavController = UINavigationController(rootViewController: MessagesController())
    messagesNavController.tabBarItem.title = "Messages"



    let postsVC = PostsViewController()
    postsVC.tabBarItem.title = "Posts"


    let organiserVC = OrganiserViewController()
    organiserVC.tabBarItem.title = "Organiser"

    let mapVC = MapViewController()
    mapVC.tabBarItem.title = "Map"
    let mapNavigationController = UINavigationController(rootViewController: mapVC)



    viewControllers = [messagesNavController, postsVC, organiserVC, mapVC]
}

}

0 个答案:

没有答案