当用户在 Swift 3 中删除时,如何清除地图视图中的方向?
How to clear map view of directions when user deletes in Swift 3?
我需要一些帮助来解决这个问题。因此,我有一个逐向导航,当用户搜索地点时,它会提供逐向导航。但是,当用户删除地名时,它仍然显示在地图视图中。当用户删除地点或地址名称时,有没有办法删除地图视图?谢谢您的帮助。
这是我的代码:
import UIKit
import MapKit
import CoreLocation
import AVFoundation
class NavigateVC: UIViewController {
@IBOutlet weak var directionLabel: UILabel!
@IBOutlet weak var search: UISearchBar!
@IBOutlet weak var mapa: MKMapView!
let locationManager = CLLocationManager()
var currentCoordinate: CLLocationCoordinate2D!
var steps = [MKRouteStep]()
let speechSynthesizer = AVSpeechSynthesizer()
var stepCounter = 0
override func viewDidLoad() {
super.viewDidLoad()
locationManager.requestAlwaysAuthorization()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
locationManager.startUpdatingLocation()
// Show an alert view to the user - as a new feature to the app.
let alert = UIAlertController(title: "Welcome", message: "You can now navigate to your favorite place with turn-by-turn navigation. Simply, enter a place name or full address to navigate to your destination. Note: Multiple destination navigation will show on the map view. Choose your destination wisely. ", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
switch action.style{
case .default:
print("default")
case .cancel:
print("cancel")
case .destructive:
print("destructive")
}}))
self.present(alert, animated: true, completion: nil)
}
func getDirections(to destination: MKMapItem) {
let sourcePlacemark = MKPlacemark(coordinate: currentCoordinate)
let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
let directionsRequest = MKDirectionsRequest()
directionsRequest.source = sourceMapItem
directionsRequest.destination = destination
directionsRequest.transportType = .automobile
let directions = MKDirections(request: directionsRequest)
directions.calculate { (response, _) in
guard let response = response else { return }
guard let primaryRoute = response.routes.first else { return }
self.mapa.add(primaryRoute.polyline)
self.locationManager.monitoredRegions.forEach({ self.locationManager.stopMonitoring(for: [=11=]) })
self.steps = primaryRoute.steps
for i in 0 ..< primaryRoute.steps.count {
let step = primaryRoute.steps[i]
print(step.instructions)
print(step.distance)
let region = CLCircularRegion(center: step.polyline.coordinate,
radius: 20,
identifier: "\(i)")
self.locationManager.startMonitoring(for: region)
let circle = MKCircle(center: region.center, radius: region.radius)
self.mapa.add(circle)
}
let initialMessage = "In \(self.steps[0].distance) meters, \(self.steps[0].instructions) then in \(self.steps[1].distance) meters, \(self.steps[1].instructions)."
self.directionLabel.text = initialMessage
let speechUtterance = AVSpeechUtterance(string: initialMessage)
self.speechSynthesizer.speak(speechUtterance)
self.stepCounter += 1
}
}
}
extension NavigateVC: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
manager.stopUpdatingLocation()
guard let currentLocation = locations.first else { return }
currentCoordinate = currentLocation.coordinate
mapa.userTrackingMode = .followWithHeading
}
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
print("ENTERED")
stepCounter += 1
if stepCounter < steps.count {
let currentStep = steps[stepCounter]
let message = "In \(currentStep.distance) meters, \(currentStep.instructions)"
directionLabel.text = message
let speechUtterance = AVSpeechUtterance(string: message)
speechSynthesizer.speak(speechUtterance)
} else {
let message = "Arrived at destination"
directionLabel.text = message
let speechUtterance = AVSpeechUtterance(string: message)
speechSynthesizer.speak(speechUtterance)
stepCounter = 0
locationManager.monitoredRegions.forEach({ self.locationManager.stopMonitoring(for: [=11=]) })
}
}
}
extension NavigateVC: UISearchBarDelegate {
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.endEditing(true)
let localSearchRequest = MKLocalSearchRequest()
localSearchRequest.naturalLanguageQuery = searchBar.text
let region = MKCoordinateRegion(center: currentCoordinate, span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))
localSearchRequest.region = region
let localSearch = MKLocalSearch(request: localSearchRequest)
localSearch.start { (response, _) in
guard let response = response else { return }
guard let firstMapItem = response.mapItems.first else { return }
self.getDirections(to: firstMapItem)
}
}
}
extension NavigateVC: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if overlay is MKPolyline {
let renderer = MKPolylineRenderer(overlay: overlay)
renderer.strokeColor = .blue
renderer.lineWidth = 10
return renderer
}
if overlay is MKCircle {
let renderer = MKCircleRenderer(overlay: overlay)
renderer.strokeColor = .red
renderer.fillColor = .red
renderer.alpha = 0.5
return renderer
}
return MKOverlayRenderer()
}
}
使用这个功能
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
}
这样每次serachbar文本发生变化时,都会调用这个函数。您可以在需要时重新加载地图。我想这就是您要找的。
我需要一些帮助来解决这个问题。因此,我有一个逐向导航,当用户搜索地点时,它会提供逐向导航。但是,当用户删除地名时,它仍然显示在地图视图中。当用户删除地点或地址名称时,有没有办法删除地图视图?谢谢您的帮助。
这是我的代码:
import UIKit
import MapKit
import CoreLocation
import AVFoundation
class NavigateVC: UIViewController {
@IBOutlet weak var directionLabel: UILabel!
@IBOutlet weak var search: UISearchBar!
@IBOutlet weak var mapa: MKMapView!
let locationManager = CLLocationManager()
var currentCoordinate: CLLocationCoordinate2D!
var steps = [MKRouteStep]()
let speechSynthesizer = AVSpeechSynthesizer()
var stepCounter = 0
override func viewDidLoad() {
super.viewDidLoad()
locationManager.requestAlwaysAuthorization()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
locationManager.startUpdatingLocation()
// Show an alert view to the user - as a new feature to the app.
let alert = UIAlertController(title: "Welcome", message: "You can now navigate to your favorite place with turn-by-turn navigation. Simply, enter a place name or full address to navigate to your destination. Note: Multiple destination navigation will show on the map view. Choose your destination wisely. ", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
switch action.style{
case .default:
print("default")
case .cancel:
print("cancel")
case .destructive:
print("destructive")
}}))
self.present(alert, animated: true, completion: nil)
}
func getDirections(to destination: MKMapItem) {
let sourcePlacemark = MKPlacemark(coordinate: currentCoordinate)
let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
let directionsRequest = MKDirectionsRequest()
directionsRequest.source = sourceMapItem
directionsRequest.destination = destination
directionsRequest.transportType = .automobile
let directions = MKDirections(request: directionsRequest)
directions.calculate { (response, _) in
guard let response = response else { return }
guard let primaryRoute = response.routes.first else { return }
self.mapa.add(primaryRoute.polyline)
self.locationManager.monitoredRegions.forEach({ self.locationManager.stopMonitoring(for: [=11=]) })
self.steps = primaryRoute.steps
for i in 0 ..< primaryRoute.steps.count {
let step = primaryRoute.steps[i]
print(step.instructions)
print(step.distance)
let region = CLCircularRegion(center: step.polyline.coordinate,
radius: 20,
identifier: "\(i)")
self.locationManager.startMonitoring(for: region)
let circle = MKCircle(center: region.center, radius: region.radius)
self.mapa.add(circle)
}
let initialMessage = "In \(self.steps[0].distance) meters, \(self.steps[0].instructions) then in \(self.steps[1].distance) meters, \(self.steps[1].instructions)."
self.directionLabel.text = initialMessage
let speechUtterance = AVSpeechUtterance(string: initialMessage)
self.speechSynthesizer.speak(speechUtterance)
self.stepCounter += 1
}
}
}
extension NavigateVC: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
manager.stopUpdatingLocation()
guard let currentLocation = locations.first else { return }
currentCoordinate = currentLocation.coordinate
mapa.userTrackingMode = .followWithHeading
}
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
print("ENTERED")
stepCounter += 1
if stepCounter < steps.count {
let currentStep = steps[stepCounter]
let message = "In \(currentStep.distance) meters, \(currentStep.instructions)"
directionLabel.text = message
let speechUtterance = AVSpeechUtterance(string: message)
speechSynthesizer.speak(speechUtterance)
} else {
let message = "Arrived at destination"
directionLabel.text = message
let speechUtterance = AVSpeechUtterance(string: message)
speechSynthesizer.speak(speechUtterance)
stepCounter = 0
locationManager.monitoredRegions.forEach({ self.locationManager.stopMonitoring(for: [=11=]) })
}
}
}
extension NavigateVC: UISearchBarDelegate {
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.endEditing(true)
let localSearchRequest = MKLocalSearchRequest()
localSearchRequest.naturalLanguageQuery = searchBar.text
let region = MKCoordinateRegion(center: currentCoordinate, span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))
localSearchRequest.region = region
let localSearch = MKLocalSearch(request: localSearchRequest)
localSearch.start { (response, _) in
guard let response = response else { return }
guard let firstMapItem = response.mapItems.first else { return }
self.getDirections(to: firstMapItem)
}
}
}
extension NavigateVC: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if overlay is MKPolyline {
let renderer = MKPolylineRenderer(overlay: overlay)
renderer.strokeColor = .blue
renderer.lineWidth = 10
return renderer
}
if overlay is MKCircle {
let renderer = MKCircleRenderer(overlay: overlay)
renderer.strokeColor = .red
renderer.fillColor = .red
renderer.alpha = 0.5
return renderer
}
return MKOverlayRenderer()
}
}
使用这个功能
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
}
这样每次serachbar文本发生变化时,都会调用这个函数。您可以在需要时重新加载地图。我想这就是您要找的。