如何解决每次我滚动离开当前位置时,MapKit都会自动缩放回当前位置
我已经用X代码构建了一个基本应用程序,该应用程序可以显示用户的位置并自动放大到用户的位置。我希望能够在地图上移动,因为我将在用户的位置周围设置重要的注释,但是每次导航到地图的不同部分时,我都会被拖回到放大的位置。建议我“ 处理地图上的平移手势,当地图处于平移状态时,请不要缩放。您可以使用简单的bool标志来控制此缩放与否。” ”有关如何解决此问题的信息。
import UIKit
导入MapKit 导入CoreLocation
ViewController类:UIViewController,CLLocationManagerDelegate,MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
private let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
//delegates
locationManager.delegate = self
self.mapView.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.startUpdatingLocation()
self.mapView.showsUserLocation = true
}
//zoom
func mapView(_ mapView: MKMapView,didUpdate userLocation: MKUserLocation) {
let region = MKCoordinateRegion(center: mapView.userLocation.coordinate,span: MKCoordinateSpan(latitudeDelta: 0.2,longitudeDelta: 0.2))
mapView.setRegion(region,animated: true)
}
}
解决方法
设置locationManager.startUpdatingLocation()
时,设备的位置硬件(GPS /无线电接收器等)开始实际尝试确定位置的工作。但是,它将继续执行此操作,直到您将其告知locationManager.stopUpdatingLocation()
,这样,每次调用该方法时,您的应用程序都会收到持续的位置更新流,这些更新都由您的mapView委托函数mapView didUpdate userLocation
处理您正在指示mapView缩放到用户的位置,因此它正在与用户试图同时执行的其他任何操作(例如滚动,缩放等)进行斗争并超越其他操作。
位置更新流中的每个位置测量值(+/- 60m或类似值)中都会有一个不确定度(.horizontalAccuracy
)的数字,并且希望每个后续更新的不确定性都比前一个更小不能保证,具体取决于本地物理环境。
一种常见的方法是在mapView didUpdate userLocation
中保留最近更新的副本,并将其与每个后续更新进行比较;如果新更新比先前更新更准确,则保存新的,否则丢弃。计算新旧之间的距离,当该距离小于您先前确定的最小值(kCLLocationAccuracyBest
或500m,50m,10m等)时,则locationManager.stopUpdatingLocation()
在您决定停止更新位置之前,应避免进行任何类型的自动缩放,因为这种行为会使用户感到沮丧。
这是一种典型的方法,但是需要针对您的特定用途进行修改:
func locationManager(_ manager: CLLocationManager,didUpdateLocations locations: [CLLocation]) {
let newLocation = locations.last!
// We've been passed a cached result so ignore and continue.
if newLocation.timestamp.timeIntervalSinceNow < -5 {
return
}
// horizontalAccuracy < 0 indicates invalid result so ignore and continue.
if newLocation.horizontalAccuracy < 0 {
return
}
// Calculate the distance between the new and previous locations.
var distance = CLLocationDistance(Double.greatestFiniteMagnitude)
if let location = location { // location is my previously stored value.
distance = newLocation.distance(from: location)
}
// If newLocation is more accurate than the previous (if previous exists) then use it.
if location == nil || location!.horizontalAccuracy > newLocation.horizontalAccuracy {
lastLocationError = nil
location = newLocation
// When newLocation's accuracy is better than our desired accuracy then stop.
if newLocation.horizontalAccuracy <= locationManager.desiredAccuracy {
stopLocationManager()
}
} else if distance < 5 {
let timeInterval = newLocation.timestamp.timeIntervalSince(location!.timestamp)
if timeInterval > 10 {
stopLocationManager()
}
}
print(newLocation)
}
有时,更新根本不会导致满足我们要求的准确性,因此其中有一个计时器,可以在10秒后终止位置搜索。
我建议您引入一个功能相同的按钮,而不是使用自动缩放用户的位置。这样一来,用户就可以准确知道行为发生的时间和原因,并且如果他们选择缩放到自己的位置,则不会同时滚动。
,不确定你是否得到了答案,但我是这样处理的。
我只是添加了一个名为 tracking 的变量,它在开始时设置为 true。 我在 viewDidLoad 中调用 locationManager.stopUpdatingLocation(),然后将跟踪变量设置为 false。
npx react-native run ios
在我的 checkLocationServices() 中,它调用 checkLocationAuthorization() 我加了
super.viewDidLoad()
checkLocationServices()
customers = ReadTextFile()
for customer in customers {
addCustomPin(customer:customer)
}
locationManager.stopUpdatingLocation()
tracking = false
这样,应用第一次调用 checkLocationServices 时,我的跟踪变量为真,因此它会检查位置。
如果我想重新打开设置,我只需要将变量重新设置为 true。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。