ios - CLLocation Manager startUpdatingLocation 未启动
<p><p>我一直在关注本教程 <a href="http://www.devfright.com/ios-6-core-location-tutorial/" rel="noreferrer noopener nofollow">http://www.devfright.com/ios-6-core-location-tutorial/</a>将位置服务添加到我的 ios 应用程序。我已经在我的 TestFileViewController.h 文件中导入了 Corelocation,如下所示:</p>
<pre><code> #import <UIKit/UIKit.h>
#import <FacebookSDK/FacebookSDK.h>
#import <CoreLocation/CoreLocation.h>
@interface TestFileViewController : UIViewController <UITableViewDelegate,UITableViewDataSource, NSStreamDelegate>
@property (nonatomic, strong) CLLocationManager *locationManager;
@end
</code></pre>
<p>然后在我添加的testFileViewController.m ViewDidLoad中(记得实现CLLocationManagerDelegate):</p>
<pre><code>//start checking for location from CoreLocation Framework (currently not working)
if ()
{
NSLog(@"location services enabled");
_locationManager = [ init];
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.delegate = self;
;
};
}
</code></pre>
<p>(条件 CLLocationManager 执行 true 和 NSLogs)然后在 ViewDidLoad 正下方:</p>
<pre><code> //fail to find location, handle error
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [
initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
;
}
//show found location, return NSLOG for proof
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation: (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"Made it to location Manager");
CLLocation *location = ;
CLLocationCoordinate2D coordinate = ;
NSLog(@"%f",coordinate.latitude);
}
</code></pre>
<p>我添加了 NSLogs 作为标记来从控制台确定函数是否被调用,但是 didFailWithError 和 didUpdateToLocation 都没有被调用。 </p>
<p>是的,我知道 didUpdateToLocation 已弃用/过时,我将其替换为 didUpdateLocations,但它仍然无法正常工作,因此我将其改回。</p>
<p>如何让我的 locationManager 开始更新当前位置并以字符串形式返回坐标值,以便稍后在我的应用程序中使用?谢谢。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>让您的类(class)成为 CLLocationManagerDelegate 的代表。</p>
<pre><code>@interface TestFileViewController : UIViewController <UITableViewDelegate,UITableViewDataSource, NSStreamDelegate, **CLLocationManagerDelegate**>
</code></pre>
<p>然后将delegate设置为self。</p>
<p>我还建议创建一个单独的类来处理位置。更简洁的设计。</p>
<p>还要确保您在主线程上调用 startUpdatingLocation。</p>
<pre><code>dispatch_sync(dispatch_get_main_queue(), ^{
});
</code></pre></p>
<p style="font-size: 20px;">关于ios - CLLocation Manager startUpdatingLocation 未启动,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/24685446/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/24685446/
</a>
</p>
页:
[1]