ios8 地图不能定位问题的解决办法

本文深入探讨了iOS开发过程中的关键技术和Swift语言的应用实践,包括界面设计、数据管理、性能优化等方面,提供了丰富的实例代码和开发技巧。
- (void)startTrackingLocation {
    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    if (status == kCLAuthorizationStatusNotDetermined) {
        [_locationManager requestWhenInUseAuthorization];
    }
    else if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusAuthorizedAlways) {
        [_locationManager startUpdatingLocation];
    }
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    switch (status) {
        case kCLAuthorizationStatusAuthorizedAlways:
        case kCLAuthorizationStatusAuthorizedWhenInUse:
            NSLog(@"Got authorization, start tracking location");
            [self startTrackingLocation];
            break;
        case kCLAuthorizationStatusNotDetermined:
            [_locationManager requestWhenInUseAuthorization];
        default:
            break;
    }
}


使用时

if (isIOS8) {
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        [self startTrackingLocation];
    }

另外,在项目的plist文件里设置key, 可以自定义授权弹窗的提示语。


上面的代码截取自苹果官方demo

/*
    Copyright (C) 2014 Apple Inc. All Rights Reserved.
    See LICENSE.txt for this sample’s licensing information
    
    Abstract:
    
                Primary view controller for what is displayed by the application.
                In this class we receieve location updates from Core Location, convert them to x,y coordinates so that they map on the imageView
                and move the pinView to that location
            
*/

#import "AAPLViewController.h"
#import "AAPLCoordinateConverter.h"

@interface AAPLViewController () <CLLocationManagerDelegate>

@property (nonatomic, weak) IBOutlet UIImageView *imageView;
@property (nonatomic, weak) IBOutlet UIImageView *pinView;
@property (nonatomic, weak) IBOutlet UIImageView *radiusView;

@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) AAPLCoordinateConverter *coordinateConverter;

@property CGFloat displayScale;
@property CGPoint displayOffset;

@property (nonatomic) AAPLGeoAnchorPair anchorPair;

@end

@implementation AAPLViewController

- (void)viewDidLoad {
	[super viewDidLoad];

	// Setup a reference to location manager.
	self.locationManager = [[CLLocationManager alloc] init];
	self.locationManager.delegate = self;
	self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    self.locationManager.activityType = CLActivityTypeOther;

	// We setup a pair of anchors that will define how the floorplan image, maps to geographic co-ordinates
    AAPLGeoAnchor anchor1 = {
        .latitudeLongitude = CLLocationCoordinate2DMake(37.770511, -122.465810),
        .pixel = CGPointMake(12, 18)
    };

    AAPLGeoAnchor anchor2 = {
        .latitudeLongitude = CLLocationCoordinate2DMake(37.769125, -122.466356),
        .pixel = CGPointMake(481, 815)
    };

    self.anchorPair = (AAPLGeoAnchorPair) {
        .fromAnchor = anchor1,
        .toAnchor = anchor2
    };

	// Initialize the coordinate system converter with two anchor points.
	self.coordinateConverter = [[AAPLCoordinateConverter alloc] initWithAnchors:self.anchorPair];
}

- (void)viewDidAppear:(BOOL)animated {
    [self setScaleAndOffset];
    [self startTrackingLocation];
}

- (void) setScaleAndOffset {
    CGSize imageViewFrameSize = self.imageView.frame.size;
    CGSize imageSize = self.imageView.image.size;

    // Calculate how much we'll be scaling the image to fit on screen.
    self.displayScale = MIN(imageViewFrameSize.width / imageSize.width, imageViewFrameSize.height / imageSize.height);
    NSLog(@"Scale Factor: %f", self.displayScale);

    // Depending on whether we're constrained by width or height,
    // figure out how much our floorplan pixels need to be offset to adjust for the image being centered
    if (imageViewFrameSize.width / imageSize.width < imageViewFrameSize.height / imageSize.height) {
        NSLog(@"Constrained by width");
        self.displayOffset = CGPointMake(0, (imageViewFrameSize.height - imageSize.height * self.displayScale) / 2);
    } else {
        NSLog(@"Constrained by height");
        self.displayOffset = CGPointMake((imageViewFrameSize.width - imageSize.width * self.displayScale) / 2, 0);
    }

    NSLog(@"Offset: %f, %f", self.displayOffset.x, self.displayOffset.y);
}

- (void)startTrackingLocation {
	CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    if (status == kCLAuthorizationStatusNotDetermined) {
		[self.locationManager requestWhenInUseAuthorization];
    }
    else if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusAuthorizedAlways) {
		[self.locationManager startUpdatingLocation];
    }
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
	switch (status) {
		case kCLAuthorizationStatusAuthorizedAlways:
		case kCLAuthorizationStatusAuthorizedWhenInUse:
			NSLog(@"Got authorization, start tracking location");
			[self startTrackingLocation];
            break;
		case kCLAuthorizationStatusNotDetermined:
			[self.locationManager requestWhenInUseAuthorization];
		default:
			break;
	}
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    // Pass location updates to the map view.
	[locations enumerateObjectsUsingBlock:^(CLLocation *location, NSUInteger idx, BOOL *stop) {
        NSLog(@"Location (Floor %@): %@", location.floor, location.description);
		[self updateViewWithLocation:location];
	}];
}

- (void) updateViewWithLocation: (CLLocation *) location {
	// We animate transition from one position to the next, this makes the dot move smoothly over the map
	[UIView animateWithDuration:0.75 animations:^ {
		// Call the converter to find these coordinates on our floorplan.
		CGPoint pointOnImage = [self.coordinateConverter pointFromCoordinate:location.coordinate];

		// These coordinates need to be scaled based on how much the image has been scaled
		CGPoint scaledPoint = CGPointMake(pointOnImage.x * self.displayScale + self.displayOffset.x,
										  pointOnImage.y * self.displayScale + self.displayOffset.y);

		// Calculate and set the size of the radius
		CGFloat radiusFrameSize = location.horizontalAccuracy * self.coordinateConverter.pixelsPerMeter * 2;
		self.radiusView.frame = CGRectMake(0, 0, radiusFrameSize, radiusFrameSize);

		// Move the pin and radius to the user's location
		self.pinView.center = scaledPoint;
		self.radiusView.center = scaledPoint;
	}];
}

- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
	// Upon rotation, we want to resize the image and center it appropriately.
    [self setScaleAndOffset];
}


转载于:https://my.oschina.net/u/1418722/blog/318751

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值