Ios 如何设置自定义距离过滤器,是否可以在Swift中设置?
我是新来的Ios 如何设置自定义距离过滤器,是否可以在Swift中设置?,ios,swift,mapkit,Ios,Swift,Mapkit,我是新来的Swift,我试着用这个 不知道为什么此代码不起作用: //start mehtod out of scope lazy var locationManager: CLLocationManager! = { let locationManager = CLLocationManager() //configeration for user location access //The delegate object to receive update
Swift
,我试着用这个
不知道为什么此代码不起作用:
//start mehtod out of scope
lazy var locationManager: CLLocationManager! = {
let locationManager = CLLocationManager()
//configeration for user location access
//The delegate object to receive update events.
locationManager.delegate = self
//The receiver does its best to achieve the requested accuracy
//locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.distanceFilter = 10.0
//Requests permission to use location services while the app is in the foreground
locationManager.requestWhenInUseAuthorization()
//allow update notification when apps stay on background
locationManager.allowsBackgroundLocationUpdates = true
return locationManager
}()
当我设置时,它工作正常:
locationManager.desiredAccuracy = kCLLocationAccuracyBest
那么我想要什么:
我想在每250米后获得LAT和LONG,如果用户改变位置,则连续15分钟调用一种方法
pushLocation(lat:double,long:double)
根据您的问题,您希望在每250米之后获得
LAT
和LONG
?
因此,这里是目标C代码
:(感谢链接:)
首先调用此方法并存储此新位置,然后您需要连续查找其他lat和long当任何(lat,long)与此存储位置匹配时,则表示您已行驶250米:
以下是SWIFT3.0代码:
func locationByMovingDistance(distanceMeters: Double, withBearing bearingDegrees: CLLocationDirection) -> CLLocation {
let distanceRadians: Double = distanceMeters / (6372797.6)
// earth radius in meters
let bearingRadians: Double = bearingDegrees * M_PI / 180
var lat1: Float = self.coordinate.latitude * M_PI / 180
var lon1: Float = self.coordinate.longitude * M_PI / 180
var lat2: Float = asin(sin(lat1) * cos(distanceRadians) + cos(lat1) * sin(distanceRadians) * cos(bearingRadians))
var lon2: Float = lon1 + atan2(sin(bearingRadians) * sin(distanceRadians) * cos(lat1), cos(distanceRadians) - sin(lat1) * sin(lat2))
return CLLocation(latitude: lat2 * 180 / M_PI, longitude: lon2 * 180 / M_PI)
}
如果您有任何进一步的问题,请随时发表评论。希望能有所帮助。这是我正在使用的代码-全部在Swift 3.0中 locationManager的此设置将考虑您所追求的距离过滤和精度:
lazy var locationManager: CLLocationManager = {
[unowned self] in
var _locationManager = CLLocationManager()
_locationManager.delegate = self
_locationManager.desiredAccuracy = [Your Value For trackingAccuracy - see below]
_locationManager.distanceFilter = [Your Value For the filter i.e., 250 for 250 meters]
_locationManager.allowsBackgroundLocationUpdates = true
_locationManager.pausesLocationUpdatesAutomatically = false
_locationManager.activityType = .fitness
return _locationManager
}()
精度设置将来自位置管理器中的预定义设置,例如,KCLLOCATIONACURACYNEARESTENMERS或KCLLOCATIONACURACYHUNDREDMERS
允许后台更新的选项允许您获得新的积分,即使您的应用程序不在前台。如果手机会不时停止移动,则必须自动关闭“暂停”-否则,如果用户停止休息5分钟而无法重新打开,则会自动关闭“捕获”-完成后必须重置这些设置
授权状态应在单独的检查中处理,如下所示,以便您可以在尚未授予授权的情况下请求授权:
if CLLocationManager.authorizationStatus() != .authorizedAlways // Check authorization for location tracking
{
locationManager.requestAlwaysAuthorization() // Will callbackdidChange... once user responds
} else {
locationManager.startUpdatingLocation()
}
由于获得授权有延迟,如果您必须发出请求,您需要等待请求它开始更新位置,直到收到locationManager的回复,您可以按如下操作:
@nonobjc func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
switch status
{
case .authorizedAlways:
locationManager.startUpdatingLocation()
default:
[Whatever you want to do - you can't get locations]
}
}
我需要.authorized.ways,您可能会逃脱。authorized.whenuse取决于您的用例
我在locationManager返回给我的位置中检查准确性字段的基础上,添加了一个额外的准确性过滤器。注意,有单独的水平和垂直精度值(以米为单位的置信距离);如果要使用高程值,则需要注意第二个精度值,因为iPhone中的高程本质上不太准确
我还检查新的点是在前一点之后捕获的-有时会出现无序值并指示“问题”值。我已经读到,您可能会得到小于0的精度值来指示问题,因此您可能希望在使用该位置之前检查该值,尽管我没有看到该问题。代码如下:
// Called back by location manager - passes arrays of new CLLocation points captured. With distanceFilter set to 250, this will get called for a change of 250M. You'll want to save the value for your timed pushLocation function.
// This function needs to be trim as it continues to be called when TrailHead is in the background and, if
// we take too long we'll get killed by iOS.
var savePosition: CLLocationCoordinate2D?
private var latestTimeProcessed = Date() // Initialize to ensure any point accepted received after initialization
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
for location in locations {
if latestTimeProcessed.compare(location.timeStamp) == .orderedAscending // Out of seq indicates a locationManager problem - discard
&& location.horizontalAccuracy < [Whatever accuracy limit you want to use] // Applying a user-selected quality filter
{
latestTimeProcessed = location.timeStamp // Used to discard any earlier timestamped points returned by the location manager
[PROCESS THE POSITION HERE - E.G.]
savePosition = locations.last
}
}
}
希望这有帮助…您是否在项目设置中设置了所有必需的权限?是的,当我设置locationManager.desiredAccuracy=KCallocationAccuracy时,它工作正常。您想要什么距离过滤器?分享确切的description@ShobhakarTiwari250米。我要过滤的距离U要250作为直径I要获得LAT和LONG在每250米后如果用户改变位置,则连续15分钟调用方法pushLocation(LAT:double,LONG:double)我已经注意到在第一个代码块中设置过滤器的位置为250米,并在我发布的答案编辑的最后一个代码块中添加了计时器代码。latestTimeProcessed我不明白如何声明它只是一个日期属性;为了声明和初始化它,我使用了:private var latestTimeProcessed=Date(),我将在上面添加它。很高兴我能提供帮助-希望苹果能让他们的API文档在这样的事情上更有用,但在他们分享我们通过StackExchange学到的知识之前,这是关键(我在这里找到了将其组合在一起的部分)。
private var myTimer: Timer? = nil
private var longInterval = 900.0 // 900 seconds = 15 minutes
override func viewDidLoad()
{
....
myTimer = Timer(timeInterval: timerInterval, target: myself, selector: #selector(myself.pushLocation), userInfo: nil, repeats: true)
RunLoop.current.add(myTimer!, forMode: RunLoopMode.commonModes)
....
}
pushLocation(lat:double,long:double){
[Use savePosition.latitude, savePosition.longitude]
}