Ios 无论我如何操作,MKMapView和locationManager始终返回0,0
我尝试了大约100种不同的解决方案,但locationManager始终返回坐标0,0 而且。。。从来没有,从来没有叫过didUpdateLocations: 我找到了一个密码,确保手机上的所有东西都正常打开。。。还是没什么 我可以做一些与源代码无关的错误吗 我最近的一次尝试使用了一个按钮,它可以使整个事情处于运动状态,但正如我所说的。。还是0,0。。它可以放大并移动到该位置 发生了什么事。。使用最新软件或其他东西的iPhone 4s上的位置不起作用吗 一些不起作用的代码。。。。 HIos 无论我如何操作,MKMapView和locationManager始终返回0,0,ios,mkmapview,cllocationmanager,Ios,Mkmapview,Cllocationmanager,我尝试了大约100种不同的解决方案,但locationManager始终返回坐标0,0 而且。。。从来没有,从来没有叫过didUpdateLocations: 我找到了一个密码,确保手机上的所有东西都正常打开。。。还是没什么 我可以做一些与源代码无关的错误吗 我最近的一次尝试使用了一个按钮,它可以使整个事情处于运动状态,但正如我所说的。。还是0,0。。它可以放大并移动到该位置 发生了什么事。。使用最新软件或其他东西的iPhone 4s上的位置不起作用吗 一些不起作用的代码。。。。 H 我真的很想
我真的很想知道我的问题的答案,我觉得我在这里尝试了一切,已经到了极限。代码没有问题,尽管有点凌乱。。向下滚动查看校样。是否由于某种原因未调用代理您不允许应用程序使用位置服务。转到设置/隐私/位置服务,找到您的应用程序以允许位置服务。看起来是这样的: 以下是工作代码的证明: --编辑 并确保将CoreLocation和Mapkit框架导入到项目中,如下所示:
我从这个网站复制了代码,创建了一个新的应用程序,并将代码粘贴到。。而且它一直在起作用。。。所以问题解决了
感谢您的努力。在
…didUpdateLocations:
中打印的内容?您不应该期望在调用startUpdatingLocation后location立即可用。必须使用didUpdateLocations委托方法(或MKMapView的didUpdateUserLocation)。如果未调用didUpdateLocations:
方法,请同时实现didFailWithError
,并查看错误(如果有)。此外,在模拟器菜单中,当应用程序运行时,您可以转到调试->位置并尝试高速公路驾驶——它应该开始发送模拟位置更新。didFailWithError或didUpdateLocations:从未触发,并且我没有任何可见的指示说明原因。高速公路选择不起任何作用,就像所有的模拟都是从美国开始的,如果我按下我做的按钮,它会转到坐标0,0来回答卡尔·维亚齐。。。它不打印任何东西。。代码永远不会到达那里。。它只是拒绝触发任何东西。我从来没有因为任何原因让任何位置触发器运行过,包括didfail。。一个。我检查了我的手机,但是我的应用没有出现在设置列表中。我编辑了我的答案,你是否将CoreLocation和Mapkit框架添加到了你的项目中?
#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
-(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);
}