1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > ios8 地图不能定位问题的解决办法

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

时间:2019-06-22 09:45:08

相关推荐

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

独角兽企业重金招聘Python工程师标准>>>

-(void)startTrackingLocation{CLAuthorizationStatusstatus=[CLLocationManagerauthorizationStatus];if(status==kCLAuthorizationStatusNotDetermined){[_locationManagerrequestWhenInUseAuthorization];}elseif(status==kCLAuthorizationStatusAuthorizedWhenInUse||status==kCLAuthorizationStatusAuthorizedAlways){[_locationManagerstartUpdatingLocation];}}-(void)locationManager:(CLLocationManager*)managerdidChangeAuthorizationStatus:(CLAuthorizationStatus)status{switch(status){casekCLAuthorizationStatusAuthorizedAlways:casekCLAuthorizationStatusAuthorizedWhenInUse:NSLog(@"Gotauthorization,starttrackinglocation");[selfstartTrackingLocation];break;casekCLAuthorizationStatusNotDetermined:[_locationManagerrequestWhenInUseAuthorization];default:break;}}

使用时

if(isIOS8){_locationManager=[[CLLocationManageralloc]init];_locationManager.delegate=self;[selfstartTrackingLocation];}

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

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

/*Copyright(C)AppleInc.AllRightsReserved.SeeLICENSE.txtforthissample’slicensinginformationAbstract:Primaryviewcontrollerforwhatisdisplayedbytheapplication.InthisclasswereceievelocationupdatesfromCoreLocation,convertthemtox,ycoordinatessothattheymapontheimageViewandmovethepinViewtothatlocation*/#import"AAPLViewController.h"#import"AAPLCoordinateConverter.h"@interfaceAAPLViewController()<CLLocationManagerDelegate>@property(nonatomic,weak)IBOutletUIImageView*imageView;@property(nonatomic,weak)IBOutletUIImageView*pinView;@property(nonatomic,weak)IBOutletUIImageView*radiusView;@property(nonatomic,strong)CLLocationManager*locationManager;@property(nonatomic,strong)AAPLCoordinateConverter*coordinateConverter;@propertyCGFloatdisplayScale;@propertyCGPointdisplayOffset;@property(nonatomic)AAPLGeoAnchorPairanchorPair;@end@implementationAAPLViewController-(void)viewDidLoad{[superviewDidLoad];//Setupareferencetolocationmanager.self.locationManager=[[CLLocationManageralloc]init];self.locationManager.delegate=self;self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;self.locationManager.activityType=CLActivityTypeOther;//Wesetupapairofanchorsthatwilldefinehowthefloorplanimage,mapstogeographicco-ordinatesAAPLGeoAnchoranchor1={.latitudeLongitude=CLLocationCoordinate2DMake(37.770511,-122.465810),.pixel=CGPointMake(12,18)};AAPLGeoAnchoranchor2={.latitudeLongitude=CLLocationCoordinate2DMake(37.769125,-122.466356),.pixel=CGPointMake(481,815)};self.anchorPair=(AAPLGeoAnchorPair){.fromAnchor=anchor1,.toAnchor=anchor2};//Initializethecoordinatesystemconverterwithtwoanchorpoints.self.coordinateConverter=[[AAPLCoordinateConverteralloc]initWithAnchors:self.anchorPair];}-(void)viewDidAppear:(BOOL)animated{[selfsetScaleAndOffset];[selfstartTrackingLocation];}-(void)setScaleAndOffset{CGSizeimageViewFrameSize=self.imageView.frame.size;CGSizeimageSize=self.imageView.image.size;//Calculatehowmuchwe'llbescalingtheimagetofitonscreen.self.displayScale=MIN(imageViewFrameSize.width/imageSize.width,imageViewFrameSize.height/imageSize.height);NSLog(@"ScaleFactor:%f",self.displayScale);//Dependingonwhetherwe'reconstrainedbywidthorheight,//figureouthowmuchourfloorplanpixelsneedtobeoffsettoadjustfortheimagebeingcenteredif(imageViewFrameSize.width/imageSize.width<imageViewFrameSize.height/imageSize.height){NSLog(@"Constrainedbywidth");self.displayOffset=CGPointMake(0,(imageViewFrameSize.height-imageSize.height*self.displayScale)/2);}else{NSLog(@"Constrainedbyheight");self.displayOffset=CGPointMake((imageViewFrameSize.width-imageSize.width*self.displayScale)/2,0);}NSLog(@"Offset:%f,%f",self.displayOffset.x,self.displayOffset.y);}-(void)startTrackingLocation{CLAuthorizationStatusstatus=[CLLocationManagerauthorizationStatus];if(status==kCLAuthorizationStatusNotDetermined){[self.locationManagerrequestWhenInUseAuthorization];}elseif(status==kCLAuthorizationStatusAuthorizedWhenInUse||status==kCLAuthorizationStatusAuthorizedAlways){[self.locationManagerstartUpdatingLocation];}}-(void)locationManager:(CLLocationManager*)managerdidChangeAuthorizationStatus:(CLAuthorizationStatus)status{switch(status){casekCLAuthorizationStatusAuthorizedAlways:casekCLAuthorizationStatusAuthorizedWhenInUse:NSLog(@"Gotauthorization,starttrackinglocation");[selfstartTrackingLocation];break;casekCLAuthorizationStatusNotDetermined:[self.locationManagerrequestWhenInUseAuthorization];default:break;}}-(void)locationManager:(CLLocationManager*)managerdidUpdateLocations:(NSArray*)locations{//Passlocationupdatestothemapview.[locationsenumerateObjectsUsingBlock:^(CLLocation*location,NSUIntegeridx,BOOL*stop){NSLog(@"Location(Floor%@):%@",location.floor,location.description);[selfupdateViewWithLocation:location];}];}-(void)updateViewWithLocation:(CLLocation*)location{//Weanimatetransitionfromonepositiontothenext,thismakesthedotmovesmoothlyoverthemap[UIViewanimateWithDuration:0.75animations:^{//Calltheconvertertofindthesecoordinatesonourfloorplan.CGPointpointOnImage=[self.coordinateConverterpointFromCoordinate:location.coordinate];//ThesecoordinatesneedtobescaledbasedonhowmuchtheimagehasbeenscaledCGPointscaledPoint=CGPointMake(pointOnImage.x*self.displayScale+self.displayOffset.x,pointOnImage.y*self.displayScale+self.displayOffset.y);//CalculateandsetthesizeoftheradiusCGFloatradiusFrameSize=location.horizontalAccuracy*self.coordinateConverter.pixelsPerMeter*2;self.radiusView.frame=CGRectMake(0,0,radiusFrameSize,radiusFrameSize);//Movethepinandradiustotheuser'slocationself.pinView.center=scaledPoint;self.radiusView.center=scaledPoint;}];}-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientationduration:(NSTimeInterval)duration{//Uponrotation,wewanttoresizetheimageandcenteritappropriately.[selfsetScaleAndOffset];}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。