LoginSignup
0
1

More than 5 years have passed since last update.

MKMapView

Posted at

创建地图

#import <MapKit/MapKit.h>

1.创建地图对象,设为属性,方便使用

@property (nonatomic,strong) MKMapView *mapView;

    self.mapView =[[MKMapViewalloc]
    initWithFrame:self.view.bounds];
    self.mapView.delegate = self;
    [self.view addSubview:self.mapView];

2.设置经纬度坐标,地图区域

    //设置经纬度坐标
    CLLocationCoordinate2D coordinate2D = CLLocationCoordinate2DMake(30.662221, 104.041367);
    //设置地图范围
    MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01);
    //设置整个显示的区域
    MKCoordinateRegion region = MKCoordinateRegionMake(coordinate2D, span);
    [self.mapView setRegion:region];

做完以上操作便能出现地图了。

实时的更新地图

1.创建位置管理器,设为属性
@property (nonatomic,strong) CLLocationManager *manager;

  self.manager = [[CLLocationManager alloc] init];
    //设置代理
    self.manager.delegate = self;
    //更新事件的最小距离
    self.manager.distanceFilter = 10.0;
    //在plist文件中添加一个价值对NSLocationAlwaysUsageDescription : YES
    //授权
    [self.manager requestAlwaysAuthorization];
    [self.manager startUpdatingLocation];
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    //获取更新后的位置信息
    CLLocation *loc = [locations firstObject];

    [UIView animateWithDuration:1.0 animations:^{
        //重新设置地图区域
        [self.mapView setRegion:MKCoordinateRegionMake(loc.coordinate, MKCoordinateSpanMake(0.01, 0.01))];
    }];
}

0
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1