Мы используем Mapkit в приложении с быстрым 4.2. Мы установили навигацию, используя route и mkdirection;
Теперь, если пользователь следует по маршруту в середине дороги, он отображает маркер текущего местоположения только на маршруте, но если мы ведем машину немного влево или вправо отдорога, то есть разница между маршрутом и маркером текущего местоположения.
Мы хотим установить маркер текущего местоположения на маршруте только во время навигации.
Пожалуйста, проверьте скриншот ниже.
Мы не уверены, как это сделать, но мы пытались сопоставить текущее местоположение и близлежащее местоположение маршрута, но в этом случае карта непрерывно мерцает.
//Set Current location code
self.anotaionSource.coordinate = self.currentCoordinate
self.anotaionSource.imageName = "carIcon"
self.mapView.addAnnotation(self.anotaionSource)
func startRoute() {
self.mapView.showsUserLocation = false
if let userLocation = self.mapView.view(for: self.mapView.userLocation) {
userLocation.isHidden = true
}
isRouteStarted = true
UserDefaults.standard.set(isRouteStarted, forKey: "ROUTE_STARTED")
UserDefaults.standard.synchronize()
btnClose.isHidden = true
if steps.count > 0{
for i in 0 ..< self.steps.count {
let step = self.steps[i]
let region = CLCircularRegion(center: step.polyline.coordinate,
radius: 50,
identifier: "\(i)")
region.notifyOnEntry = true
region.notifyOnExit = true
self.locationManager.startMonitoring(for: region)
}
mapView.setUserTrackingMode(.followWithHeading, animated:true)
var initialMessage = ""
if self.steps[1].distance > 1000.0{
initialMessage = "\(String(format: "%.1f", self.steps[1].distance/1000)) km, \(self.steps[1].instructions)."
self.directionsLabel.text = initialMessage
} else {
initialMessage = "\(String(format: "%.0f", self.steps[1].distance)) meters, \(self.steps[1].instructions)."
self.directionsLabel.text = initialMessage
}
let speechUtterance = AVSpeechUtterance(string: initialMessage)
speechUtterance.rate = AVSpeechUtteranceDefaultSpeechRate
let lang = "en-US"
speechUtterance.voice = AVSpeechSynthesisVoice(language: lang)
self.speechSynthesizer.speak(speechUtterance)
self.setImageAccordingToDirection(instruction: initialMessage)
self.sendDataToBLE()
self.stepCounter += 1
btnStart.backgroundColor = .red
btnStart.setImage(#imageLiteral(resourceName: "Stop"), for: .normal)
btnStart.tag = 1
}
}
// MARK : Route end
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// manager.stopUpdatingLocation()
let getAngle = angle(fromCoordinate: oldLocation, toCoordinate: currentLocation.coordinate)
UIView.animate(withDuration: 2, animations: {
self.anotaionSource.coordinate = currentLocation.coordinate
let annotationView = self.mapView?.view(for: self.anotaionSource)
annotationView?.transform = CGAffineTransform(rotationAngle: CGFloat(getAngle))
})
}
func angle(fromCoordinate first: CLLocationCoordinate2D, toCoordinate second: CLLocationCoordinate2D) -> Float {
let deltaLongitude = Float(second.longitude - first.longitude)
let deltaLatitude = Float(second.latitude - first.latitude)
let angle: Float = (.pi * 0.5) - atan(deltaLatitude / deltaLongitude)
if deltaLongitude > 0 {
return angle
} else if deltaLongitude < 0 {
return angle + .pi
} else if deltaLatitude < 0 {
return .pi
}
return 0.0
}