我试图用cllocation和mkreversegeocoder来修改城市名称.在我的viewdidload方法我istance cllocationmanager:
self.locManager = [[CLLocationManager alloc] init];
locManager.delegate = self;
locManager.desiredAccuracy = kCLLocationAccuracyBest;
[locManager startUpdatingLocation];
Run Code Online (Sandbox Code Playgroud)
之后:
- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation
*)newLocation
fromLocation:(CLLocation *)oldLocation {
//some code to retrieve my information
MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc]
initWithCoordinate:newLocation.coordinate ];
geoCoder.delegate = self;
[geoCoder start];
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark
*)placemark
{
MKPlacemark *myPlacemark = placemark;
citta.text = [placemark locality];
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error{
citta.text =@ "Unknow";
Run Code Online (Sandbox Code Playgroud)
应用程序仅在我第一次获得我的价值时起作用.在第二次应用程序崩溃.我认为是因为地理编码器已启动,我认为我必须只有一个运行.(但我真的不是这个......).在哪种方式我可以控制地理编码器正在运行?
我已经看到我可以使用cllocationcoordinate2d在我的viewdidload中使用mkreversegeocoder但是...以这种方式我可以检索newlocation.coordinate?
我已经部分解决了将地理编码器声明为类变量并进行检查的问题
if (self.geocoder != nil) {
[geocoder release];
}
Run Code Online (Sandbox Code Playgroud)
但是....如果在我的
- …Run Code Online (Sandbox Code Playgroud) 我开始乱用CLLocationManager,我正在尝试使用下面的回调方法.我去外面在我的iPhone上测试它,并且在最初的几次更新中,我似乎得到了胡言乱语的价值,就像突然我的距离是39米,即使我没有移动到任何地方.或者有时它会从5开始,然后跳到20米,再没有我移动到任何地方.我走到外面走了,从最初的起点开始的更新似乎"没问题",然后当我走回去时,我回到原来的39米起点.我想知道我在下面做的是否正确.我还包括了我的viewDidLoad方法,我初始化了我的CLLocationManager对象.
有没有办法确保我的第一个值是准确的?提前致谢.
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if (startingPoint == nil) {
NSLog(@"NIL starting point");
self.startingPoint = newLocation;
}
CLLocationDistance distance = [newLocation distanceFromLocation:startingPoint];
NSString *distanceString = [[NSString alloc] initWithFormat:@"%g m", distance];
distanceLabel.text = distanceString;
[distanceString release];
numberOfUpdates++;
NSString *countString = [[NSString alloc] initWithFormat:@"%d", numberOfUpdates];
countLabel.text = countString;
[countString release];
}
Run Code Online (Sandbox Code Playgroud) 我有关于重大变更位置服务的问题.
Apple文档说"无论您使用标准位置服务还是重要更改位置服务来获取位置事件,您收到这些事件的方式都是相同的."
但是在"重大变更位置服务"的情况下,我无法获得任何"标准位置服务"的回复.如果有人有任何意见,请告诉我?
我目前正在开发一个使用CLLocationManager且精度最低的应用程序(kCLLocationAccuracyThreeKilometers).但是,每隔几分钟我需要获得一个准确的用户位置,因此我在计时器到期时将精度设置为kCLLocationAccuracyBest.
显然,CLLocationManager开始更新位置,从不准确的位置到更准确的位置.但问题是,我不知道如何检测何时达到最佳位置精度,所以我永远不知道何时有最佳位置可供使用.
有人知道这个问题的解决方案吗?非常感谢.
我想知道用户是否接受了该应用以检查其位置.如果"location = nil"用户未获得授权,这是真的吗?
coreLocationManager = [[CLLocationManager alloc] init];
[coreLocationManager setDistanceFilter:kCLDistanceFilterNone];
[coreLocationManager setDesiredAccuracy:kCLLocationAccuracyBest];
location = [coreLocationManager location];
[coreLocationManager startUpdatingLocation];
Run Code Online (Sandbox Code Playgroud) 我正在编写一个移动iOS应用程序,它使用iOS重要位置更改服务来管理大量地理围栏,在我们从CoreLocation Framework收到此消息时启用和禁用地理围栏.
在Apple开发者网站上,它说:
重要更改位置服务仅在设备位置发生重大变化(例如500米或更长)时才会提供更新.
真正让我失望的是"500米或更多",究竟什么是重要的位置变更服务,我们实际拥有什么保证?
此API是否可靠地跟踪区域之间的变化,以便在收到消息时我们知道是时候重新分配我们的地理围栏(假设我们每500米半径的地理围栏少于20个)?
当用户按下按钮时,我想将其经度和经度存储为变量.我不明白如何使用Coordinate2D方法.而不是println(locManager.location)我想打印(或存储为单独的变量)纬度和经度.请帮忙.我的代码如下.
class ViewController: UIViewController, CLLocationManagerDelegate {
var locManager = CLLocationManager()
@IBOutlet var pushToParkText: UILabel!
@IBAction func carButton(sender: UIButton) {
println(locManager.location)
}
@IBOutlet var labelLatitude: UILabel!
@IBOutlet var labelLongitude: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Core Location Manager asks for GPS location
locManager.delegate = self
locManager.desiredAccuracy = kCLLocationAccuracyBest
locManager.requestWhenInUseAuthorization()
locManager.startMonitoringSignificantLocationChanges()
// Check if the user allowed authorization
if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse ||
CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Authorized)
{
println(locManager.location)
} else {
labelLatitude.text = "Location not authorized"
labelLongitude.text = "Location not authorized" …Run Code Online (Sandbox Code Playgroud) 我在我的iPad App中使用CLLocationManager类来获取当前位置.
我使用下面的代码来获取地址详细信息,
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSUserDefaults *oIsLocation = [NSUserDefaults standardUserDefaults];
if([oIsLocation boolForKey:@"IsLocation"])
{
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
if (locationAge > 5.0) return;
if (newLocation.horizontalAccuracy < 0) return;
if (bestEffortAtLocation == nil || bestEffortAtLocation.horizontalAccuracy > newLocation.horizontalAccuracy)
{
if(bestEffortAtLocation.coordinate.latitude != newLocation.coordinate.latitude && bestEffortAtLocation.coordinate.longitude != newLocation.coordinate.longitude)
{
[self saveLocation:newLocation];
self.bestEffortAtLocation = newLocation;
[self saveUserLocation];
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在进行反向地理编码以获取详细信息如下:
NSLog(@"placemark.ISOcountryCode %@",placemark.ISOcountryCode);
NSLog(@"placemark.country %@",placemark.country);
NSLog(@"placemark.postalCode %@",placemark.postalCode);
NSLog(@"placemark.administrativeArea %@",placemark.administrativeArea);
NSLog(@"placemark.locality %@",placemark.locality);
NSLog(@"placemark.subLocality %@",placemark.subLocality);
NSLog(@"placemark.subThoroughfare %@",placemark.subThoroughfare);
Run Code Online (Sandbox Code Playgroud)
placemark.locality给出了城市名称,但没有全名,如北卡罗来纳州只有NC.
当我的客户在美国使用应用程序时,而不是像"北卡罗莱纳州"这样的全名,他们获得NC,圣查尔斯获得圣查尔斯等.
有没有办法获得全名?
出于某种原因,Xcode认为我没有实现协议的didFailWithError方法CLLocationManagerDelegate
我没有看到我做错了什么,因为我从另一个SO帖子中复制了一下,说这个didFailWithError方法是为Swift 3更新的.所以我不明白为什么Xcode认为我没有实现didFailWithError
任何见解将不胜感激!
class OptionsViewController: UIViewController,
CLLocationManagerDelegate {
var locationManager: CLLocationManager = CLLocationManager()
override func viewDidLoad() {
//Ask user for location
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
//Use users current location if no starting point set
if CLLocationManager.locationServicesEnabled() {
if CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse
|| CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways {
locationManager.requestLocation()
}
else{
locationManager.requestWhenInUseAuthorization()
}
}
else{
//Alert user to open location service, bra bra bra here...
}
}
func locationManager(_ manager: …Run Code Online (Sandbox Code Playgroud) 根据文档 CLLocationManagerDelegate
从启动相应位置服务的线程中调用委托对象的方法。该线程本身必须具有活动的运行循环,就像在应用程序主线程中找到的那样。
我不清楚这是否意味着要在后台线程上接收位置管理器更新,我们必须在该后台线程上实例化位置管理器还是直接startUpdatingLocation()在该线程上调用方法。
无论如何,这解释了当a 从后台线程上启动时没有收到来自的任何事件时的问题:CLLocationManagerDelegateCLLocationManager
该线程本身必须具有活动的运行循环
如果我理解运行循环的功能正常,那么所有NSThreads运行循环都会实例化,但是只有在为线程分配一些工作后,运行循环才会运行。因此,要CLLocationManager在后台线程上正确发送事件,我们需要将线程的运行循环设置为永久循环,以便它可以在CLLocationManager调用到达时处理它们。
这个问题提出了一种确保运行循环正在运行的合理解决方案,但是作者暗示这是一种处理器昂贵的方式。
另外,根据线程文档,
就内存使用和性能而言,线程化会给您的程序(和系统)带来实际成本
我很欣赏我们都通过使用Grand Central Dispatch来使用大量线程,但是Grand Central Dispatch可能会减轻其内部线程管理中的许多麻烦。
所以我的第一个问题是,是否值得通过连续运行的运行循环设置后台线程,以便在后台线程上处理位置事件,或者与离开管理器相比,这会涉及不合理的额外处理量吗?在主线程上?
其次,如果值得的话,是否有使用Grand Central Dispatch进行此操作的好方法。据我了解的文档,Grand Central Dispatch管理自己的线程,而我们无法知道给定块将在哪个线程上执行。我想我们可以简单地执行通常的运行循环代码,以使我们CLLocationManager实例化的任何线程的运行循环都在循环中连续运行,但是这可能不会影响独立分配给Grand Central Dispatch的其他任务吗?
multithreading nsthread grand-central-dispatch cllocationmanager swift
ios ×8
cllocation ×3
iphone ×3
location ×2
swift ×2
geolocation ×1
nsthread ×1
objective-c ×1
swift3 ×1
xcode ×1