基于位置的应用程序使用带有按钮的滑动汉堡菜单来过滤地图 (iOS MapKit) 搜索结果 Xcode
Location Based App using a sliding hamburger menu with buttons that will filter map (iOS MapKit) search results Xcode
我在 Xcode 上使用 MapKit 创建一个 "nearby" hospital/mental 健康 center/call 中心位置应用程序。我希望用户能够过滤地图的结果。例如,如果我正在寻找医院,我只想查看地图上显示的医院位置。到目前为止,为此,我创建了一个滑动式汉堡菜单,其中包含类似复选框的按钮以用作过滤器。因此,例如,您点击汉堡菜单按钮并选中医院复选框,这样您就只会在地图上获得医院结果。但是,我一定是搞砸了,因为当我到达 MapViewController 时,只出现黑屏。请告诉我如何解决这个问题,或者完全使用不同的方法来实现相同的目标。
import UIKit
import MapKit
class MapViewController: NSObject, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var trailingMap: NSLayoutConstraint!
@IBOutlet weak var leadingMap: NSLayoutConstraint!
func viewDidLoad() {
mapView.delegate = self as? MKMapViewDelegate
let locationManager = CLLocationManager()
let regionRadius:CLLocationDistance = 1000
locationManager.requestWhenInUseAuthorization()
mapView.showsUserLocation = true
locationManager.startUpdatingLocation()
}
var hamburgerMenuIsVisible = false
@IBAction func hamburgerMenu(_ sender: Any) {
if hamburgerMenuIsVisible {
leadingMap.constant = 150
trailingMap.constant = -150
hamburgerMenuIsVisible = true
} else {
leadingMap.constant = 0
trailingMap.constant = 0
hamburgerMenuIsVisible = false
}
}
var mapItems: [MKMapItem] = []
var place = MKLocalSearch.Request() //user's facility search options
var naturalLanguageQuery: String? {
place.naturalLanguageQuery = "hospital"
let place2 = MKLocalSearch.Request()
place2.naturalLanguageQuery = "Mental Health Facility"
let place3 = MKLocalSearch.Request()
place3.naturalLanguageQuery = "Women's Center"
let place4 = MKLocalSearch.Request()
place4.naturalLanguageQuery = "Call Center"
place.region = self.mapView.region
place2.region = self.mapView.region
place3.region = self.mapView.region
place4.region = self.mapView.region
return place.naturalLanguageQuery
}
@IBAction func hospitalButton(_ sender: Any) {
print(mapItems)
}
@IBAction func callCenterButton(_ sender: Any) {
print(mapItems)
}
}
你的 ViewController 是 UIViewController 的子类,所以你必须使用 UIViewController 的继承而不是 NSObject
所以,替换这个
class MapViewController: NSObject, CLLocationManagerDelegate {
有了这个
class MapViewController: UIViewController, CLLocationManagerDelegate {
还可以通过在 viewDidLoad 函数的 func
关键字之前添加 override
关键字来修复您的 viewDidLoad 方法。那是因为您覆盖了 UIViewController 中的函数。您必须在此函数的开头调用 super.viewDidLoad()
。所以 viewDidLoad 应该是这样的
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self as? MKMapViewDelegate
let locationManager = CLLocationManager()
let regionRadius:CLLocationDistance = 1000
locationManager.requestWhenInUseAuthorization()
mapView.showsUserLocation = true
locationManager.startUpdatingLocation()
}
我在 Xcode 上使用 MapKit 创建一个 "nearby" hospital/mental 健康 center/call 中心位置应用程序。我希望用户能够过滤地图的结果。例如,如果我正在寻找医院,我只想查看地图上显示的医院位置。到目前为止,为此,我创建了一个滑动式汉堡菜单,其中包含类似复选框的按钮以用作过滤器。因此,例如,您点击汉堡菜单按钮并选中医院复选框,这样您就只会在地图上获得医院结果。但是,我一定是搞砸了,因为当我到达 MapViewController 时,只出现黑屏。请告诉我如何解决这个问题,或者完全使用不同的方法来实现相同的目标。
import UIKit
import MapKit
class MapViewController: NSObject, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var trailingMap: NSLayoutConstraint!
@IBOutlet weak var leadingMap: NSLayoutConstraint!
func viewDidLoad() {
mapView.delegate = self as? MKMapViewDelegate
let locationManager = CLLocationManager()
let regionRadius:CLLocationDistance = 1000
locationManager.requestWhenInUseAuthorization()
mapView.showsUserLocation = true
locationManager.startUpdatingLocation()
}
var hamburgerMenuIsVisible = false
@IBAction func hamburgerMenu(_ sender: Any) {
if hamburgerMenuIsVisible {
leadingMap.constant = 150
trailingMap.constant = -150
hamburgerMenuIsVisible = true
} else {
leadingMap.constant = 0
trailingMap.constant = 0
hamburgerMenuIsVisible = false
}
}
var mapItems: [MKMapItem] = []
var place = MKLocalSearch.Request() //user's facility search options
var naturalLanguageQuery: String? {
place.naturalLanguageQuery = "hospital"
let place2 = MKLocalSearch.Request()
place2.naturalLanguageQuery = "Mental Health Facility"
let place3 = MKLocalSearch.Request()
place3.naturalLanguageQuery = "Women's Center"
let place4 = MKLocalSearch.Request()
place4.naturalLanguageQuery = "Call Center"
place.region = self.mapView.region
place2.region = self.mapView.region
place3.region = self.mapView.region
place4.region = self.mapView.region
return place.naturalLanguageQuery
}
@IBAction func hospitalButton(_ sender: Any) {
print(mapItems)
}
@IBAction func callCenterButton(_ sender: Any) {
print(mapItems)
}
}
你的 ViewController 是 UIViewController 的子类,所以你必须使用 UIViewController 的继承而不是 NSObject
所以,替换这个
class MapViewController: NSObject, CLLocationManagerDelegate {
有了这个
class MapViewController: UIViewController, CLLocationManagerDelegate {
还可以通过在 viewDidLoad 函数的 func
关键字之前添加 override
关键字来修复您的 viewDidLoad 方法。那是因为您覆盖了 UIViewController 中的函数。您必须在此函数的开头调用 super.viewDidLoad()
。所以 viewDidLoad 应该是这样的
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self as? MKMapViewDelegate
let locationManager = CLLocationManager()
let regionRadius:CLLocationDistance = 1000
locationManager.requestWhenInUseAuthorization()
mapView.showsUserLocation = true
locationManager.startUpdatingLocation()
}