天天看点

MKMapView 地图 + 混合地图 + 卫星地图

MKMapView

//CLLocationManager —> 模拟定位

//地理编码

//通过地名 —> 正向地理编码

//通过经纬度 —> 反向地理编码

import “ViewController.h”

import MapKit/MapKit.h

@interface ViewController ()

@property (nonatomic,strong) MKMapView *mapView;

@property (nonatomic,strong) CLLocationManager *locationManager;

@end

@implementation ViewController

  • (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@”地图”,@”混合地图”,@”卫星地图”]];

    [seg addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];

    self.navigationItem.titleView = seg;

    self.mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];

    //经纬度

    //深大 22.533367,113.935404

    //设置地图显示的区域

    [self.mapView setRegion:MKCoordinateRegionMake(CLLocationCoordinate2DMake(22.533367, 113.935404), MKCoordinateSpanMake(0.01, 0.01))];

    //显示定位图表 —> 必须地图允许显示

    self.mapView.showsUserLocation = YES;

    [self.view addSubview:self.mapView];

    self.locationManager = [[CLLocationManager alloc] init];

    //精确度 越高 越耗电

    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    //定位的频率 单位米

    self.locationManager.distanceFilter = 10;

    //设置代理

    self.locationManager.delegate = self;

    //ios8 以后设置访问定位的权限

    if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)

    {

    //NSLocationAlwaysUsageDescription plist中加入该key

    [self.locationManager requestAlwaysAuthorization];

    }

    //开启定位

    [self.locationManager startUpdatingLocation];

}

//定位完成后的代理方法

//location 定位完成的位置会放到最后一个元素中.

- (void)locationManager:(CLLocationManager *)manager

didUpdateLocations:(NSArray *)location

{

CLLocation *newLocation = [location lastObject];

//停止定位
[self.locationManager stopUpdatingLocation];

[self.mapView setRegion:MKCoordinateRegionMake(newLocation.coordinate, MKCoordinateSpanMake(0.1, 0.1)) animated:YES];
           

}

-(void)change:(UISegmentedControl *)s

{

switch (s.selectedSegmentIndex) {

case 0:

self.mapView.mapType = MKMapTypeStandard;

break;

case 1:

self.mapView.mapType = MKMapTypeHybrid;

break;

case 2:

self.mapView.mapType = MKMapTypeSatellite;

break;

default:
        break;
}
           

}

继续阅读