MKMapView和locationManager总是返回0,0 nomatter我是怎么做的

时间:2014-07-13 15:33:44

标签: ios mkmapview cllocationmanager

我尝试了大约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);
}

我真的很欣赏我的问题的答案,感觉我在这里尝试了一切,并且在我的绳索尽头。

2 个答案:

答案 0 :(得分:0)

代码没什么问题,虽然有点乱 - 向下滚动查看证明。是否由于某种原因没有调用代理您未允许该应用使用位置服务。转到设置/隐私/位置服务,找到您的应用以允许位置服务。它看起来像这样:

locationServices

以下是工作代码的证明:

proofWorkingCode

- 编辑 并确保将CoreLocation和Mapkit框架导入到您的项目中:

framework

答案 1 :(得分:0)

我从这个网站复制了代码,创建了一个新的应用程序并将代码粘贴在..并且它工作imedieltly ...所以问题解决了。

感谢您的努力。