我正在尝试获取设备的航向信息,以便利用磁航向和真实航向之间的差异来确定用户位置的磁偏角。为此,我有一个 Helper 类和我的 MainVc (mvc)。在我的辅助类 init 方法中,我执行以下操作:
...
...
locationManager = CLLocationManager()
switch CLLocationManager.authorizationStatus() {
case .AuthorizedAlways:
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
case .NotDetermined:
locationManager.requestAlwaysAuthorization()
case .AuthorizedWhenInUse, .Restricted, .Denied:
mvc.alertDenied();
}
if (!initialized)
{
initialized = true;
self.performSelector("finishUpdatingLocation", withObject: nil, afterDelay: 2.0);
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print ("didUpdateLocations")
var userLocation:CLLocation = locations[0] as! CLLocation
longitude = Float(userLocation.coordinate.longitude);
latitude = Float(userLocation.coordinate.latitude);
}
func finishUpdatingLocation () {
locationManager.stopUpdatingLocation();
NSObject.cancelPreviousPerformRequestsWithTarget(self, selector: "finishUpdatingLocation", object: nil);
mvc.goingToUpdateHeading();
locationManager.startUpdatingHeading(); …Run Code Online (Sandbox Code Playgroud) 即使应用程序被用户杀死,我也想跟踪用户位置。我已经尝试过下面的代码,但在应用程序关闭时不起作用。
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
self.checkUsersLocationServicesAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.allowsBackgroundLocationUpdates = true
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
extension ViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
for location in locations {
let objr = ["lat": "\(location.coordinate.latitude)",
"logn": "\(location.coordinate.longitude)",
"date": self.getCurrentDateAndTime()]
self.saveToCoreDataAndFetchBack(objr as NSDictionary)
//self.TriggerNotification()
}
}
}
Run Code Online (Sandbox Code Playgroud) 我试图找到一种方法来改变使用CLLocationManager构建的iphone应用程序,但其设置为千米.有没有办法将它改为里程?
这是我第一次使用CLLocationManager.我不确定我做的是否正确.如果我错了,请纠正我.
我在我的viewDidLoad方法中初始化locationManager 并以相同的方法告诉locationManager startUpdatingLocation.
当代表收到时
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
[locationManager stopUpdatingLocation];
//do stuff with the coordinates
}
Run Code Online (Sandbox Code Playgroud)
避免重复调用此委托方法.
我有一个UIButton用户可以点击更新位置的地方
//called by user action
-(IBAction)updateLocation{
//start updating delegate
locationManager.delegate=self;
[locationManager startUpdatingLocation];
}
Run Code Online (Sandbox Code Playgroud)
但是当位置发生变化时,当我点击时UIbutton,位置坐标根本没有变化.:(
我究竟做错了什么?这是做的权利还是我根本不应该停止locationManager?
帮助将不胜感激.
我有一个使用startMonitoringSignificantLocationChanges的应用程序.
打开此项时会出现右上角的紫色箭头.
我正在做一些测试,偶尔我会调用stopMonitoringSignificantLocationChanges.
发生这种情况时,即使我在Xcode中杀死了应用程序,仍会显示紫色箭头.我进入Settings.app - >位置服务,看到我的应用程序旁边的紫色箭头也亮了,这意味着它仍在使用它.这甚至在没有Xcode连接到设备的情况下运行,并且在我从多任务栏中杀死应用程序之后也是如此.
怎么会这样?
不应该杀死应用程序导致重要的位置变更监控停止?
这肯定发生在5.1 ...没有用5.0测试.
假设我有一个采用CLLocationCoordinate2D的方法.我无法将nil直接传递给代码中的nil; 编译器抱怨.但是,以下编译.那么在运行时会发生什么?
CLLocation* loc = nil;
[self passMeCoordinates:loc.coordinate];
Run Code Online (Sandbox Code Playgroud) 我的应用程序也在应用程序终止后使用CLLocationManager类中的方法startMonitoringSignificantLocationChanges来使用核心位置.
我的应用程序使用方法中的iOS 5和6中的位置键启动: - (BOOL)应用程序:application didFinishLaunchingWithOptions:launchOptions; 在AppDelegate类中,一切运行良好.
但在iOS-7测试版中,应用程序在重要位置更改后不会使用位置键启动.
有人遇到过这个问题吗?
我在模拟器和设备上试了一下.
谢谢您的帮助.
我正在研究我的家庭自动化应用程序.我正在使用estimotes iBeacons,我想要做的就是让我的家庭自动化控制能够知道我在家里的距离.每个iBeacon在我的家庭控制器上都有一个虚拟开关,当我接触到iBeacon时,我的设备在前台或后台将更新我的控制器以打开我的开关,当我靠近灯塔时关闭它然后当我有想远离我的灯塔.所有这一切都很完美,我很喜欢它,但是我的一些条件依赖于我在一段时间内接近,而我注意到的是几分钟后甚至在接近半小时后一个信标,iPhone 5s基本上断开它的连接,然后将其重新启动,导致它执行退出(关闭我的虚拟交换机),然后立即执行输入(转回虚拟交换机).你可以想象这对于妻子和我自己来说非常烦人,因为退出/进入,卧室的灯开始闪烁.我已经读过有关此问题的人,并尝试了我在互联网上看到的一切无济于事.
我注意到,每当为iBeacon划过监视器交叉时,都会调用didDetermineState和相应的Enter/Exit回调函数.我该怎么办才能让它停止发生?如果需要,我可以提供代码示例,但这更像是一个普遍的问题.
我需要使用我的应用程序获取15个最近用户的列表.当前用户的当前位置存储如下:
PFGeoPoint *currentLocation = [PFGeoPoint geoPointWithLocation:newLocation];
PFUser *currentUser = [PFUser currentUser];
[currentUser setObject:currentLocation forKey:@"location"];
[currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error)
{
NSLog(@"Saved Users Location");
}
}];
Run Code Online (Sandbox Code Playgroud)
现在我想通过PFQuery检索附近的用户,如下所示:
- (NSArray *)findUsersNearby:(CLLocation *)location
{
PFGeoPoint *currentLocation = [PFGeoPoint geoPointWithLocation:location];
PFQuery *locationQuery = [PFQuery queryWithClassName:@"User"];
[locationQuery whereKey:@"location" nearGeoPoint:currentLocation withinKilometers:1.0];
locationQuery.limit = 15;
NSArray *nearbyUsers = [locationQuery findObjects];
return nearbyUsers;
}
Run Code Online (Sandbox Code Playgroud)
不幸的是它不起作用.我的阵列似乎没有条目.有人可以为我清理一下,如何正确使用查询?
干杯,大卫
(也发布于:https://www.parse.com/questions/pfquery-to-retrieve-users-nearby)
嗨,我想在位置变化50米(不少于此)时呼叫网络服务。我已尝试使用重大更改,但它至少可以运行500米,并且startupdatelocations始终会调用。因此,我如何检测设备是否从该位置移动了50或100米。
在许多stackoverflow问题中,我都使用距离过滤器为50米。但是在移动到50米之前我无法获得设备中的位置更新信息。
这里有一些关于距离过滤器的解释-iPhone的核心位置:距离过滤器如何工作?
iphone ×5
objective-c ×5
ios ×4
cocoa-touch ×2
swift ×2
background ×1
ibeacon ×1
ios7 ×1
ios9 ×1
location ×1
pfquery ×1
xcode ×1