我有一个使用CoreLocation和跟踪用户移动的应用程序。
当用户步行或驾驶时,我将每个坐标保存到本地数据库(lat/lng/alt),以便我可以根据保存的坐标绘制路线。
几天前,我添加了累积海拔增益,并在中使用了此代码
didLocationUpdate:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
netElevationGain += MAX(0, oldLocation.altitude - newLocation.altitude);
}
Run Code Online (Sandbox Code Playgroud)
但有些事情就是不对劲。
我启动了应用程序,开始步行几英里,netElevationGain大约走了 500 米。这绝不是正确的。这是数据库中保存的每个点
的列表:netElevation
0,41,43,44,47,52,55,62,73,80,91,100,114,140,146,153,159,180,199,208,228,240,259,275,320,329,349,359,366,375,381,392,408,419,428,437,446,449,462,469,478,486
Run Code Online (Sandbox Code Playgroud)
海拔高度
0.000000,181.678055,181.891495,182.786850,179.315399,177.035721,182.636307,181.259399,178.653015,192.552551,185.398819,182.693436,181.369766,154.306747,157.031693,159.748871,185.080856,198.080673,176.473877,178.646851,175.784424,178.184128,181.237488,188.956894,177.713181,193.673019,188.470184,182.749054,181.966507,181.547592,191.638657,198.713989,188.582977,197.977921,203.184540,205.108856,198.304123
为了显示增益,我使用简单的select max(gain) from table where ...
更新
现在我得到更糟糕的值(按降序排列):
Alt:
200.334869,200.594666,196.293945,195.240234,191.800446,192.622375,179.951385,179.185577,179.681122,177.843719,183.459595,174.170502,0.000000
Run Code Online (Sandbox Code Playgroud)
获得:
307,301,294,285,275,269,252,246,234,227,217,202,0
Run Code Online (Sandbox Code Playgroud) 我正在编写一个使用CoreLocation. 是否可以向框架请求位置许可并且单独向框架请求位置许可?
或者我是否必须向消费应用程序请求位置许可。
我问这个问题是因为任何以上的应用程序都需要iOS 8在应用程序的.NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescriptioninfo.plist
快速回答:
您可以向框架请求位置许可(即self.locationManager.requestAlwaysAuthorization),但使用所述框架的消费应用程序必须提供其 中的NSLocationAlwaysUsageDescription/NSLocationWhenInUseUsageDescription密钥info.plist。
我试图获取用户的位置很长,但我做了以下工作:
导入的核心位置
import CoreLocation
Run Code Online (Sandbox Code Playgroud)
添加核心位置代表
class ViewController: UIViewController, CLLocationManagerDelegate, AVCaptureMetadataOutputObjectsDelegate, DTDeviceDelegate {
Run Code Online (Sandbox Code Playgroud)
定义了这个变量:
var locationManager: CLLocationManager!
Run Code Online (Sandbox Code Playgroud)
然后添加此方法:
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation:CLLocation = locations[0]
let long = userLocation.coordinate.longitude;
let lat = userLocation.coordinate.latitude;
print(long, lat)
//Do What ever you want with it
}
Run Code Online (Sandbox Code Playgroud)
但该位置未得到打印,我的方法甚至没有得到它。
我将要使用“核心位置”的项目添加到我的列表中,并且该应用在我第一次运行时要求我使用位置服务。但是现在位置正在打印中。...我在做什么错?
当我第一次运行locationManager.requestWhenInUseAuthorization()时,会弹出标准的“允许AppName在您使用应用程序时访问您的位置”,并且再也不会基于标准的IOS设计(无论用户是否选择“允许,不要允许”) )。以下代码位于主视图控制器内的ViewDidLoad中
// Check location status
if locationAuthStatus == CLAuthorizationStatus.AuthorizedWhenInUse {
self.displayMessage.hidden = false
self.displayMessage.text = "Waiting for GPS Signal..."
} else {
locationManager.requestWhenInUseAuthorization()
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,如果用户退出程序,禁用定位服务,然后再返回,则不会出现弹出窗口要求用户启用已显示的位置。因此,如果以下两个条件为真,我想添加另一个自定义弹出窗口以请求权限
最初,我在locationManager.requestWhenInUseAuthorization()之后获得了弹出代码。但是,这引起了问题。如果用户是第一次使用该应用,则系统会提示他默认的弹出窗口,然后我的弹出窗口便会出现。
谢谢,
我正在开发一个具有信标区域监控功能的应用程序。下面是监控信标区域的代码。
-(void)setBeaconMonitoringForUUID:(NSString *)strID withMajor:(NSString *)strMajor withMinor:(NSString *)strMinor withIdentifier:(NSString *)strIdentifier {
NSUUID *strUUID = [[NSUUID alloc] initWithUUIDString:strID];
CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:strUUID major:[strMajor intValue] minor:[strMinor intValue] identifier:strIdentifier];
[beaconRegion setNotifyEntryStateOnDisplay:YES];
[beaconRegion setNotifyOnEntry:YES];
[beaconRegion setNotifyOnExit:YES];
[self.objLocationManager startMonitoringForRegion:beaconRegion];
[self.objLocationManager startRangingBeaconsInRegion:beaconRegion];}
Run Code Online (Sandbox Code Playgroud)
locationManager初始化如下
- (id)init
{
self = [super init];
if (self != nil)
{
self.objLocationManager = [CLLocationManager new];
self.objLocationManager.delegate = self;
self.objLocationManager.distanceFilter = 10.0;
self.objLocationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
self.objLocationManager.allowsBackgroundLocationUpdates = YES;
if ([self.objLocationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.objLocationManager requestAlwaysAuthorization];
}
[self.objLocationManager startUpdatingLocation];
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
现在的问题是,为了监控信标区域,iOS …
我有一个name, lon, 和的字典lat。
var places = [["name": "Angkor Wat", "lon": "103.8670", "lat": "13.4125"], ["name": "Pick up zone", "lon": "103.85771740610468", "lat": "13.41250067905404"], ["name": "66-35 Otto Rd", "lon": "-73.8889131530723", "lat": "40.706725952864886"], ["name": "40 Williams St", "lon": "-71.30756271909428", "lat": "42.64240728775266"], ["name": "324 Broadway Rd", "lon": "-71.29124543680823", "lat": "42.68061286243683"], ["name": "39 Kendall Pond Rd", "lon": "-71.33234704167283", "lat": "42.867489706954636"], ["name": "269 Treble Cove Rd", "lon": "-71.30049088921143", "lat": "42.55656906569715"]]
Run Code Online (Sandbox Code Playgroud)
这是我现在拥有的代码。
import UIKit
import MapKit
import CoreLocation
class ViewAllPinsController: …Run Code Online (Sandbox Code Playgroud) 在 iOS13 上请求“始终”权限期间,用户可以点击“允许一次”,这将调用具有状态的适当委托kCLAuthorizationStatusAuthorizedWhenInUse,但再次请求“始终”会调用带有 的委托kCLAuthorizationStatusAuthorizedAlways。为什么?当其他组合像您总是请求的那样只工作一次时,您会得到它,甚至再次调用也不会调用带有状态的委托。
要测试的示例代码:
@import CoreLocation;
@interface ViewController () <CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager *locationManager;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
}
- (IBAction)doauthloc:(id)sender {
[self.locationManager requestAlwaysAuthorization];
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
switch(status) {
case kCLAuthorizationStatusNotDetermined:NSLog(@"AUTH STATUS:kCLAuthorizationStatusNotDetermined"); break;
case kCLAuthorizationStatusRestricted:NSLog(@"AUTH STATUS:kCLAuthorizationStatusRestricted"); break;
case kCLAuthorizationStatusDenied:NSLog(@"AUTH STATUS:kCLAuthorizationStatusDenied"); break;
case kCLAuthorizationStatusAuthorizedAlways:NSLog(@"AUTH STATUS:kCLAuthorizationStatusAuthorizedAlways"); break;
case kCLAuthorizationStatusAuthorizedWhenInUse:NSLog(@"AUTH STATUS:kCLAuthorizationStatusAuthorizedWhenInUse"); break;
};
}
@end
Run Code Online (Sandbox Code Playgroud) 我有我的位置(坐标)和我的航向(指南针),并且正在尝试计算朝向给定其他坐标的航向,以便我可以在屏幕上显示指向该位置的箭头。我正在构建的基本上是一个美化的指南针,它不指向北方,而是指向特定的坐标,因此需要考虑该坐标的方位和用户的指南针方向。
\n我期望 CoreLocation 包含为此所需的构建块,但除了获取两个坐标之间的距离之外,似乎没有任何东西。
\n不幸的是,我手动计算的尝试并没有取得很大成果,而且很难猜测我哪里出错了。此后,我尝试调整我在此处找到的内容,并且我在那里的示例数据中得到了相同的结果,但一旦我使用自己的坐标,它就会完全出错。
\nextension Double {\n var radians: Double {\n self * .pi / 180\n }\n}\n\n\nlet A = CLLocationCoordinate2D(latitude: 51.0295437, longitude: 13.7277793)\nlet B = CLLocationCoordinate2D(latitude: 51.026819, longitude: 13.726348)\n\nlet \xce\x94L = abs(A.longitude) - abs(B.longitude)\nlet X = cos(B.latitude.radians) * sin(\xce\x94L.radians)\nlet Y = cos(A.latitude.radians) * sin(B.latitude.radians) - sin(A.latitude.radians) * cos(B.latitude.radians) * cos(\xce\x94L.radians)\nlet bearing = atan2(X, Y)\nlet heading = bearing - userHeading\nRun Code Online (Sandbox Code Playgroud)\n一旦我将设备的标题合并到此(最后的减法)中,似乎我的值以某种方式沿 WE 轴翻转(指向左而不是右,反之亦然),但是 NS 轴似乎是正确的。方位角的计算肯定有问题。\n我真的对地理坐标系统了解不多。
\n有没有办法将经度/纬度位置(由核心位置返回)转换为地址(街道和城市)?
我试图通过简单的快速代码使用iOS模拟器获得位置.但是在调试会话中,不会调用回调函数.我将虚拟位置设置为iOS模拟器.
// ViewController.swift
// helloWorld
//
// Created by on 5/17/15.
// Copyright (c) 2015 All rights reserved.
//
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
var locMan: CLLocationManager!
var lat: CLLocationDegrees!
var lng: CLLocationDegrees!
var hasPosData: Bool
required init(coder aDecoder: NSCoder) {
locMan = CLLocationManager()
lat = CLLocationDegrees()
lng = CLLocationDegrees()
hasPosData = false
super.init(coder: aDecoder)
}
func retreiveCurrentPos(){
locMan.delegate = self
locMan.requestAlwaysAuthorization()
// start using GPS function
locMan.startUpdatingLocation()
}
func saveResult2File(){
var contents: String
let dirToSave …Run Code Online (Sandbox Code Playgroud) 我需要访问mainViewController中包含的此方法中的用户位置
-(void)loadAnnotations{
[mapView removeAnnotations:mapView.annotations];
CLLocationCoordinate2D workingCoordinate;
workingCoordinate.latitude= //here i need the users latitude
workingCoordinate.longitude= //here i need the users longitude
NSLog(@" this is %@", workingCoordinate.latitude);
iProspectLiteAnnotation *tempMine = [[iProspectLiteAnnotation alloc] initWithCoordinate:workingCoordinate];
[tempMine setTitle:@"Present Location"];
[tempMine setAnnotationType:iProspectLiteAnnotationTypeUser];
[mapView addAnnotation:tempMine];
}
Run Code Online (Sandbox Code Playgroud)
但是mainViewController已经设置为
<fipsideViewControllerDelegate>
Run Code Online (Sandbox Code Playgroud)
我应该在头文件和实现文件中添加什么来轮询位置管理器以获取用户当前的纬度和经度?
core-location ×11
ios ×9
swift ×6
objective-c ×5
mapkit ×3
bluetooth ×1
cocoa-touch ×1
ibeacon ×1
info.plist ×1
ios13 ×1
xcode ×1