苹果公司在更新了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的定位问题也就解决了。。