ios8系统地图定位问题解决

本文详细介绍了如何在iOS8系统更新后解决地图定位问题的步骤和代码实现,包括导入框架、配置plist文件、声明代理、创建地图、定位方法等关键环节。

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





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值