JG专题iOS 地图的奇技淫巧

iOS开发高级进阶(#12-13)位置、摇一摇、通知

2016-03-16  本文已影响124人  逸飞u

地理位置信息

启用CoreLocation

1.在Info.plist中写明对设备的要求

1

2.在Info.plist中选择提示用户授权的信息
可以设定When或者Always

2-1 2-2

3.在viewDidLoad中添加

    self.locationManager = [CLLocationManager new];
    [self.locationManager requestWhenInUseAuthorization];
    //or
    //[self.locationManager requestAlwaysInUseAuthorization];

4.确认是否获得用户允许

    [CLLocationManager authorizationStatus];

5.每次启动时可以先判断用户是否曾经允许过

    [CLLocationManager locationServicesEnabled];

使用CoreLocation(地址信息)

//delegate:
 @interface ViewController () <CLLocationManagerDelegate, MKMapViewDelegate>

 @property (strong, nonatomic) CLLocationManager * locationManager;

 if ( !self.locationManager ){
      self.locationManager = [CLLocationManager new];
}

self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;//精度
self.locationManager.distanceFilter = kCLDistanceFilterNone; // 任何
self.locationManager.delegate = self;
[self.locationManager requestWhenInUseAuthorization];

 //精确    
 [self.locationManager startUpdatingLocation];


 //500米以上变化,每5分钟不超过一次通知
//[self.locationManager startMonitoringSignificantLocationChanges];

//区域模式  
//[self.locationManage startMonitoringForRegion:<#(nonnull CLRegion *)#>];

//Visit 模式(略)

位置变动后的处理

//位置变动后的处理
-(void)locationManager:(CLLocationManager *)manager 
    didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations {
        CLLocation * location = locations[0];//当前位置

        self.locationLabel.text = [NSString stringWithFormat:@"Location: {%lf , %lf} ^%lf) Direction=%lf degree, Floor=%d",
                         location.coordinate.longitude,//经度
                         location.coordinate.latitude,//纬度
                         location.altitude,//海拔
                         location.course,//方向
                         (int)location.floor.level//楼层
                         ];
        }

地图显示

实现

1.打开功能(TARGETS)


打开功能

2.把MKMapView放到界面

地图坐标系理论知识

iOS里有3个坐标系

MKMapView类型
MKMapView类型
MKMapView 用法
MKMapView 标注

MKMapView -更多

      self.locationManager = [CLLocationManager new];
      [self.locationManager requestWhenInUseAuthorization];

      self.mapView.delegate = self;
      self.mapView.mapType = MKMapTypeHybridFlyover;
      self.mapView.showsUserLocation = YES;
      //self.mapView.visibleMapRect = MKMapRectMake(0, 0, 200000, 200000);
      self.mapView.region = MKCoordinateRegionMake(self.mapView.centerCoordinate,     MKCoordinateSpanMake(1, 1));
      if (!self.mapView.userLocationVisible) {
        self.mapView.centerCoordinate = self.mapView.userLocation.coordinate;//移动地图位置
        MKCircle * circle = [MKCircle circleWithCenterCoordinate:self.mapView.centerCoordinate radius:1000];
        [self.mapView addOverlay:circle];
      }

    MKCircle * circle = [MKCircle circleWithCenterCoordinate:location.coordinate radius:200];
    [self.mapView addOverlay:circle];
    [UIView animateWithDuration:1.0 animations:^{
    self.mapView.centerCoordinate = location.coordinate;
    }];

    MKPointAnnotation * point = [[MKPointAnnotation alloc] init];
    point.coordinate = location.coordinate;
    point.title = @"You are here!";
    [self.mapView addAnnotation:point];

    [self.locationManager stopUpdatingLocation];
上一篇下一篇

猜你喜欢

热点阅读