希望有人可以帮助我解决这个问题:对于我正在构建的iOS应用程序(在Swift 5中),我放置了一个MapKitView,该MapKitView在打开时会显示我的当前位置。然后,我按下按钮直接打开Apple Maps应用程序。一切都在这一点上工作,但我还必须添加一个功能,当Maps打开时,就像我正在编写例如,自动执行基于查询的搜索。假设搜索栏上的“加油站”。
代码如下:
import MapKit
import CoreLocation
class MapScreen: UIViewController {
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var addressLabel: UILabel!
let locationmanager = CLLocationmanager()
let regionInmeters: Double = 10000
var previousLocation: CLLocation?
let geoCoder = CLGeocoder()
var directionsArray: [mkdirections] = []
override func viewDidLoad() {
super.viewDidLoad()
checkLocationServices()
}
func setupLocationmanager() {
locationmanager.delegate = self
locationmanager.desiredaccuracy = kCLLocationaccuracyBest
}
func centerViewOnUserLocation() {
if let location = locationmanager.location?.coordinate {
let region = MKCoordinateRegion.init(center: location,latitudinalMeters: regionInmeters,longitudinalMeters: regionInmeters)
mapView.setRegion(region,animated: true)
}
}
func checkLocationServices() {
if CLLocationmanager.locationServicesEnabled() {
setupLocationmanager()
checkLocationAuthorization()
} else {
}
}
func checkLocationAuthorization() {
switch CLLocationmanager.authorizationStatus() {
case .authorizedWhenInUse:
startTrackingUserLocation()
break
case .denied:
break
case .notDetermined:
locationmanager.requestWhenInUseAuthorization()
case .restricted:
break
case .authorizedAlways:
break
default:
break
}
}
func startTrackingUserLocation() {
mapView.showsUserLocation = true
centerViewOnUserLocation()
locationmanager.startUpdatingLocation()
previousLocation = getcenterLocation(for: mapView)
}
func getcenterLocation(for mapView: MKMapView) -> CLLocation {
let latitude = mapView.centerCoordinate.latitude
let longitude = mapView.centerCoordinate.longitude
return CLLocation(latitude: latitude,longitude: longitude)
}
func getDirections() {
guard let location = locationmanager.location?.coordinate else {
return
}
let request = createDirectionsRequest(from: location)
let directions = mkdirections(request: request)
directions.calculate { [unowned self] (response,error) in
guard let response = response else { return }
for route in response.routes {
let steps = route.steps
self.mapView.addOverlay(route.polyline)
self.mapView.setVisibleMapRect(route.polyline.boundingMapRect,animated: true)
}
}
}
func openmapForPlace() {
let lat1 : CLLocationDegrees = mapView.centerCoordinate.latitude
let lng1 : CLLocationDegrees = mapView.centerCoordinate.longitude
let latitude:CLLocationDegrees = lat1
let longitude:CLLocationDegrees = lng1
let regionDistance:cllocationdistance = regionInmeters
let coordinates = CLLocationCoordinate2DMake(latitude,longitude)
let regionSpan = MKCoordinateRegion(center: coordinates,longitudinalMeters: regionInmeters)
let options = [
MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center),MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)
]
let placemark = MKPlacemark(coordinate: coordinates,addressDictionary: nil)
let mapItem = MKMapItem(placemark: placemark)
let streetNumber = placemark.subThoroughfare ?? ""
let streetName = placemark.thoroughfare ?? ""
mapItem.name = "\(streetName) \(streetNumber)"
mapItem.openInmaps(launchOptions: nil)
}
func searchRequest() {
}
@IBaction func goButtonTapped(_ sender: UIButton) {
getDirections()
openmapForPlace()
}
func createDirectionsRequest(from coordinate: CLLocationCoordinate2D) -> mkdirections.Request {
let destinationCoordinate = getcenterLocation(for: mapView).coordinate
let startingLocation = MKPlacemark(coordinate: coordinate)
let destination = MKPlacemark(coordinate: destinationCoordinate)
let request = mkdirections.Request()
request.source = MKMapItem(placemark: startingLocation)
request.destination = MKMapItem(placemark: destination)
request.transportType = .automobile
request.requestsAlternateRoutes = true
return request
}
func resetMapView(withNew directions: mkdirections) {
mapView.removeOverlays(mapView.overlays)
directionsArray.append(directions)
let _ = directionsArray.map { $0.cancel() }
}
}
extension MapScreen: CLLocationmanagerDelegate {
func locationmanager(_ manager: CLLocationmanager,didChangeAuthorization status: CLAuthorizationStatus) {
checkLocationAuthorization()
}
}
extension MapScreen: mkmapviewdelegate {
func mapView(_ mapView: MKMapView,regionDidChangeAnimated animated: Bool) {
let center = getcenterLocation(for: mapView)
guard let previousLocation = self.previousLocation else { return }
guard center.distance(from: previousLocation) > 50 else { return }
self.previousLocation = center
geoCoder.cancelGeocode()
geoCoder.reverseGeocodeLocation(center) { [weak self] (placemarks,error) in
guard let self = self else { return }
if let _ = error {
//alert
return
}
guard let placemark = placemarks?.first else {
//alert
return
}
let streetNumber = placemark.subThoroughfare ?? ""
let streetName = placemark.thoroughfare ?? ""
DispatchQueue.main.async {
self.addressLabel.text = "\(streetName) \(streetNumber)"
}
}
}
func mapView(_ mapView: MKMapView,rendererFor overlay:MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(overlay: overlay as! MKPolyline)
renderer.strokeColor = .blue
return renderer
}
}