ios8系统地图定位问题解决
来源:互联网 发布:网络电影会给独立账号 编辑:程序博客网 时间:2024/05/19 20:41
苹果公司在更新了IOS8系统之后,对系统地图的定位方式做了较大变动,导致之前的定位代码无法正常使用,研究了很长时间,终于解决了,废话不多说了,直奔主题。
一、首先确保所用到的framework全部导入了:MapKit.framework(mapview使用),CoreLocation.framework(定位使用);
二、plist文件里面添加值:NSLocationAlwaysUsageDescription和NSLocationWhenInUseDescription,设成YES(两个值前后最好不要出现空格);
三、代码:首先声明两个代理:CLLocationManagerDelegate和MKMapViewDelegate;
创建地图:(ViewWidth是宏定义,获取屏幕的宽度)
_mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 64, ViewWidth, self.view.bounds.size.height - 64)]; _mapView.mapType = MKMapTypeStandard; _mapView.zoomEnabled = YES; _mapView.showsUserLocation=YES; _mapView.delegate = self; [self.view addSubview:_mapView];设置地图默认的放大倍数:(调节0.001两个值,1为原始大小)
MKCoordinateSpan theSpan; //地图的范围 越小越精确 theSpan.latitudeDelta = 0.001; theSpan.longitudeDelta = 0.001; MKCoordinateRegion theRegion; theRegion.center = [[_locationManager location] coordinate]; theRegion.span = theSpan; [_mapView setRegion:theRegion];
定位方法:
<span style="font-size:14px;">- (void)startLocating{ if([CLLocationManager locationServicesEnabled]) { _locationManager = [[CLLocationManager alloc] init]; //设置定位的精度 [_locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; _locationManager.distanceFilter = 100.0f; _locationManager.delegate = self; if ([[[UIDevice currentDevice] systemVersion] floatValue] > 8.0) { [_locationManager requestAlwaysAuthorization]; [_locationManager requestWhenInUseAuthorization]; } //开始实时定位 [_locationManager startUpdatingLocation]; }} - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ CLLocation *location = [locations lastObject]; //CLLocationCoordinate2D coordinate = location.coordinate; CLGeocoder *geocoder = [[CLGeocoder alloc] init]; [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error){ if (error) { NSLog(@"location is error"); } NSString *city; NSString *province; for (CLPlacemark *place in placemarks) { city = [place.addressDictionary objectForKey:@"City"]; province = [place.addressDictionary objectForKey:@"State"]; if(!city) { city = province; } if (!province) { province = city; } [[NSUserDefaults standardUserDefaults]setObject:province forKey:@"Province"]; [[NSUserDefaults standardUserDefaults]setObject:city forKey:@"City"]; } }]; [_locationManager stopUpdatingLocation];} -(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{ NSLog(@"经度====%f",manager.location.coordinate.longitude); NSLog(@"纬度=====%f",manager.location.coordinate.latitude); NSString *strLat = [NSString stringWithFormat:@"%f",manager.location.coordinate.latitude]; NSString *strLng = [NSString stringWithFormat:@"%f",manager.location.coordinate.longitude]; _locationStr = [NSString stringWithFormat:@"%f,%f",[strLng doubleValue],[strLat doubleValue]]; CLLocationCoordinate2D coords = CLLocationCoordinate2DMake([strLat doubleValue],[strLng doubleValue]); [_mapView setCenterCoordinate:coords animated:YES]; CLGeocoder *geocoder=[[CLGeocoder alloc]init]; [geocoder reverseGeocodeLocation:manager.location completionHandler:^(NSArray *placemark,NSError *error) { CLPlacemark *mark=[placemark objectAtIndex:0]; NSString *str = [NSString stringWithFormat:@"%@",mark]; str = [[str componentsSeparatedByString:@"<"] objectAtIndex:0]; _label.text = str; } ]; [_locationManager stopUpdatingLocation]; }</span>
第一个方法在ViewdidLoad中直接调用就行,第二个方法是获取到省和市,第三个方法中,manager.location,就是定位到得经纬度了,如代码所示,可以输出具体的值查看,
<span style="font-size:14px;"> CLLocationCoordinate2D coords = CLLocationCoordinate2DMake([strLat doubleValue],[strLng doubleValue]); [_mapView setCenterCoordinate:coords animated:YES];</span>是将地图显示到当前定位的位置。有了经纬度,就可以拿到当前具体的位置信息了:
<span style="font-size:14px;">CLGeocoder *geocoder=[[CLGeocoder alloc]init]; [geocoder reverseGeocodeLocation:manager.location completionHandler:^(NSArray *placemark,NSError *error) { CLPlacemark *mark=[placemark objectAtIndex:0]; NSString *str = [NSString stringWithFormat:@"%@",mark]; str = [[str componentsSeparatedByString:@"<"] objectAtIndex:0]; _label.text = str; } ];</span>反编码拿到位置信息,就可以用label来显示了,到此,ios8的定位问题也就解决了。。
0 0
- ios8系统地图定位问题解决
- IOS8地图定位
- iOS8地图不能定位
- ios8地图定位
- iOS8 地图定位问题
- iOS8地图定位-CoreLocation
- ios8 定位问题解决
- iOS8定位问题解决方案
- ios8 定位问题解决思路
- iOS8定位问题解决方案
- ios8 地图定位失败解决办法
- iOS8关于地图定位问题
- iOS8地图定位新用法
- iOS8无法定位问题解决方案
- ios8 系统定位问题
- ios8 系统定位问题
- ios8 系统定位问题
- ios8之后调用系统定位
- flex 4里闪烁效果
- matlab 排列组合函数的用法
- 关于iOS中用AudioFile相关API解码或播放AAC_HE_V2时只能识别单声首22.05k采样率的问题
- windows server 2008 IP安全策略关闭端口
- C语言宏定义##连接符和#符的使用
- ios8系统地图定位问题解决
- XML文件的代码提示
- iOS6 自动布局 入门–Auto Layout(转)
- log4j 日志管理
- Android SDK Android NDK Android Studio 官方下载地址
- iText操作PDF之学习笔记
- Uva1347
- leetcode--Remove Element
- 如何对修改后的java源代码进行重新打包