【发布时间】:2014-07-13 15:33:43
【问题描述】:
我尝试了大约 100 种不同的解决方案,但 locationManager 始终返回坐标 0,0。
而且...从来没有,也没有一次调用过 didUpdateLocations:
我找到了一个代码,可以确保手机上的所有东西都正常打开……仍然没有。
我可以做一些与源代码无关的错误吗?
我最近的尝试使用一个按钮来设置整个物体的运动,但正如我所说.. 仍然是 0,0.. 它会放大并完美地移动到那个位置。
这是怎么回事.. 使用最新软件或其他软件的 iPhone 4s 是否无法使用位置?
一些代码不起作用....
.h
#import <UIKit/UIKit.h>
#import<MapKit/MapKit.h>
#import<CoreLocation/CoreLocation.h>
@interface TestingMapsFirstViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
CLLocation *currentLocation;
}
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
property (weak, nonatomic) IBOutlet UIButton *locationButton;
property (strong, nonatomic) CLLocationManager *locationManager;
property (weak, nonatomic) IBOutlet NSString *test1;
property (weak, nonatomic) CLLocation *CURRENT_LOCATION;
end
.m
-(void)viewDidLoad
{
[self.mapView setMapType:MKMapTypeStandard];
[self.mapView setZoomEnabled:YES];
[self.mapView setScrollEnabled:YES];
[self.mapView setShowsUserLocation:YES];
[super viewDidLoad];
}
- (void)startUpdatingCurrentLocation
{
// if location services are on
if([CLLocationManager locationServicesEnabled])
{
// if location services are restricted do nothing
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusRestricted )
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Determining your current location cannot be performed at this time because location services are enabled but restricted." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
else
{
if(!self.locationManager)
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[self.locationManager startUpdatingLocation];
// [_locationManager setDistanceFilter:5.0f]; // measured in meters
// [_locationManager setHeadingFilter:5]; // measured in degrees
self.mapView.centerCoordinate = self.locationManager.location.coordinate;//self.mapView.userLocation.location.coordinate;
// [self.mapView setCenterCoordinate:self.mapView.userLocation.location.coordinate zoomLevel:2.0 animated:YES];
NSLog(@"user latitude = %f",_locationManager.location.coordinate.latitude);
NSLog(@"user longitude = %f",_locationManager.location.coordinate.longitude);
}
[_locationManager startUpdatingLocation];
}
}
else
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Determining your current location cannot be performed at this time because location services are not enabled." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
}
// Delegate method
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation* loc = [locations lastObject]; // locations is guaranteed to have at least one object
float latitude = loc.coordinate.latitude;
float longitude = loc.coordinate.longitude;
NSLog(@"%.8f",latitude);
NSLog(@"%.8f",longitude);
}
我真的很感激我的问题的答案,感觉我在这里尝试了所有方法并且已经走到了尽头。
【问题讨论】:
-
...didUpdateLocations:中打印了什么? -
您不应期望在调用 startUpdatingLocation 后位置立即可用。您必须使用 didUpdateLocations 委托方法(或 MKMapView 的 didUpdateUserLocation)。如果
didUpdateLocations:方法没有被调用,也执行didFailWithError并查看错误是什么,如果有的话。此外,在 Simulator 菜单中,当应用程序运行时,您可以转到 Debug -> Location 并尝试 Freeway Drive - 它应该会开始发送模拟位置更新。 -
didFailWithError 或 didUpdateLocations: 从未被触发,我没有得到任何可见的指示。
-
高速公路选择什么都不做,就像在所有模拟中总是从美国开始一样,如果我按下我所做的按钮,它会转到坐标 0,0
-
回答 Carl Veazey... 它不打印任何东西.. 代码永远不会到达那里.. 它只是拒绝触发任何东西。我从来没有设法让任何位置触发器以任何原因运行,其中包括 didfail.. 一个。
标签: ios mkmapview cllocationmanager