zoukankan      html  css  js  c++  java
  • 定位功能(使用系统地图)

    1.导入MapKit,CoreLocation库

    2.viewController文件

    #import "ViewController.h"
    #import <CoreLocation/CoreLocation.h>
    
    @interface ViewController ()<CLLocationManagerDelegate>
    {
        CLLocationManager *_locationManager;
    }
    @end
    
    @implementation ViewController
    
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        //1.创建locationManager
        _locationManager = [[CLLocationManager alloc] init];
        
        //2.info.plist文件加入字段
        //NSLocationWhenInUseDescription
        //NSLocationAlwaysUsageDescription
        
        if (![CLLocationManager locationServicesEnabled]) {
            NSLog(@"定位服务未打开");
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请打开定位服务" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
        
        //3.请求权限(一直允许访问位置信息)
        [_locationManager requestAlwaysAuthorization];
        //程序运行期间允许访问位置信息
        //[_locationManager requestWhenInUseAuthorization];
        
        //4.定位精确度
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        //设置隔多少米后更新位置信息
        _locationManager.distanceFilter = 100;
        _locationManager.delegate = self;
        
        //5.开启定位
        [_locationManager startUpdatingLocation];
    }
    
    
    
    #pragma mark --CLLocationManagerDelegate---
    //权限状态改变
    -(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
    {
        NSLog(@"%i",status);
        //给出相应提示信息
        if (status != kCLAuthorizationStatusAuthorizedAlways) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请设置程序允许访问位置信息" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
        else
        {
            [_locationManager startUpdatingLocation];
        }
    }
    //更新到用户位置信息
    -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
        CLLocation *location = [locations firstObject];
        NSLog(@"%f-%f",location.coordinate.latitude,location.coordinate.longitude);
        //反编码
        CLGeocoder *geo = [[CLGeocoder alloc] init];
        [geo reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
            NSLog(@"%@",error);
        }];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
  • 相关阅读:
    删除Tomcat服务及其它注意
    下拉菜单被js图片挡住
    There are no resources that can be added or removed from the server
    Mysql存中文值乱码
    myeclipse的项目导入到eclipse下,com.sun.org.apache.commons.beanutils.BeanUtils不能导入
    No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
    winServer2003除默认端口外的其他端口只能本地访问,关闭防火墙即可
    Oracle 11.2.0.3 on windows 2008 r2
    windows2008 r2 卸载GI
    初始化参数(Initialization Parameter)知识合集 based on 11g
  • 原文地址:https://www.cnblogs.com/liaods/p/4805378.html
Copyright © 2011-2022 走看看