• CoreLocation+MapKit系统定位(含坐标以及详细地址)


    iOS8 之后出现一些新的配置

    [self.manager requestWhenInUseAuthorization];
     并且在info.plist文件中增加
     NSLocationWhenInUseUsageDescription  BOOL YES
     NSLocationAlwaysUsageDescription         string “提示描述”
    记得加依赖库
    CoreLocation.framework MapKit.framework

    创建MapView

    if (_mapView == nil) {
        _mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, 0.5, 0.5)];
        //        _mapView.userTrackingMode = MKUserTrackingModeFollow;
        _mapView.delegate = self;
        _mapView.showsUserLocation = YES;
        [self.view addSubview:_mapView];
    }
    

    创建locationManager

    if ([CLLocationManager locationServicesEnabled] == YES) {//判断定位是否可用
        _locationManager = [[CLLocationManager alloc]init];
        _locationManager.delegate = self;
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;//定位精确度
        _locationManager.distanceFilter = 10; //位置变化10米更新位置信息
        //NSLog(@"定位");
       
        if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
            [_locationManager requestWhenInUseAuthorization];
    }else{
        //NSLog(@"定位不可用");
    }
    
     [_locationManager startUpdatingLocation];
    

    CLLocationManager Delegate

    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
        //NSLog(@"%s",__FUNCTION__);
        [manager stopUpdatingLocation];
        NSLog(@"newLocation = %@",newLocation);
        NSLog(@"oldLocation = %@",oldLocation);
        //获取经度和纬度的结构体
        CLLocationCoordinate2D coordinate = newLocation.coordinate;
        //纬度信息,CLLocationDegrees类型实际上就是double型
        CLLocationDegrees latitude = coordinate.latitude;
        //经度信息,CLLocationDegrees类型实际上就是double型
        CLLocationDegrees longitude = coordinate.longitude;
        NSLog(@"%f,%f",latitude,longitude);
        
        [self.latitude setText:[NSString stringWithFormat:@"纬度:%lf",latitude]];
        [self.longitude setText:[NSString stringWithFormat:@"经度:%lf",longitude]];
      /*
        
        // 获取当前所在的城市名
        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        //根据经纬度反向地理编译出地址信息
        [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *array, NSError *error)
         {
             if (array.count > 0)
             {
                 CLPlacemark *placemark = [array objectAtIndex:0];
                 //详细信息
                 [self.address setText:[NSString stringWithFormat:@"地址:%@",placemark.name]];
                 //获取城市
                 NSString *city = placemark.locality;
                 if (!city) {
                 //四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
                     city = placemark.administrativeArea;
                 }
                 [self.city setText:[NSString stringWithFormat:@"城市:%@",city]];
             }
             else if (error == nil && [array count] == 0)
             {
                 NSLog(@"No results were returned.");
             }
             else if (error != nil)
             {
                 NSLog(@"An error occurred = %@", error);
             }
         }];
        */
    }
    
    -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
        NSLog(@"定位失败  ,%@",error);
    }
    

    MapView获得详细地址

    - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
        
        
        CLLocation * newLocation = userLocation.location;
        self.lastCoordinate=mapView.userLocation.location.coordinate;
        
        NSUserDefaults *standard = [NSUserDefaults standardUserDefaults];
        
        [standard setObject:@(self.lastCoordinate.longitude) forKey:MMLastLongitude];
        [standard setObject:@(self.lastCoordinate.latitude) forKey:MMLastLatitude];
        
        CLGeocoder *clGeoCoder = [[CLGeocoder alloc] init];
        CLGeocodeCompletionHandler handle = ^(NSArray *placemarks,NSError *error)
        {
            for (CLPlacemark * placeMark in placemarks)
            {
                NSDictionary *addressDic=placeMark.addressDictionary;
                
                NSString *City=[addressDic objectForKey:@"City"];
    //            NSString *subLocality=[addressDic objectForKey:@"SubLocality"];
    //            NSString * adressName  = [NSString stringWithFormat:@"%@%@",subLocality,street];
                [self.city setText:[NSString stringWithFormat:@"城市:%@",City]];
                
                 [self.address setText:[NSString stringWithFormat:@"地址:%@",placeMark.name]];
                [self stopLocation];
            }
            
        };
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        [clGeoCoder reverseGeocodeLocation:newLocation completionHandler:handle];
        
    }
    

    停止定位

    -(void)stopLocation
    {
        if (_mapView) {
            NSLog(@"关闭");
            _mapView.showsUserLocation = NO;
            [_mapView removeFromSuperview];
            _mapView = nil;
        }
    }
    

      

     

    来源: http://www.cnblogs.com/spaceID/p/4992167.html

  • 相关阅读:
    07-图5 Saving James Bond
    07-图4 哈利·波特的考试(25 分)多源最短路,邻接矩阵
    最短路径问题
    最小生成树
    06-图3 六度空间(30 分)
    06-图2 Saving James Bond
    06-图1 列出连通集(25 分)邻接矩阵

    05-树9 Huffman Codes(30 分)
    集合及运算
  • 原文地址:https://www.cnblogs.com/spaceID/p/4992167.html
Copyright © 2020-2023  润新知