#import "ViewController.h"
//1.导入MKMapKit
#import <MapKit/MapKit.h>
@interface ViewController ()
- (IBAction)segmentHandle:(UISegmentedControl *)sender;
@property (nonatomic, weak)MKMapView *mapView;
@end
@implementation ViewController
- (IBAction)segmentHandle:(UISegmentedControl *)sender {
/*
MKMapTypeStandard 标准地图
MKMapTypeSatellite卫星地图
MKMapTypeHybrid 混合地图
*/
MKMapType type;
switch (sender.selectedSegmentIndex) {
case 0:
type = MKMapTypeStandard;
//标准地图
break;
case 1:
type = MKMapTypeSatellite;
//卫星地图
break;
case 2:
type = MKMapTypeHybrid;
//混合地图
break;
default:
break;
}
self.mapView.mapType = type;
}
- (MKMapView *)mapView
{
if (!_mapView) {
#if 0
MKMapView *mapView = [[MKMapView alloc]initWithFrame:self.view.bounds];
CLLocationCoordinate2D coordinate2D;
//保存经纬度对象
coordinate2D.latitude = 39.9087607470;
coordinate2D.longitude = 116.3975780490;
//保存经纬度比例
MKCoordinateSpan span;
//纬度
span.latitudeDelta = 1/100;
span.longitudeDelta = 1/100;
//范围:两部分 1.中心点经纬度 经纬度比例尺
MKCoordinateRegion region;
region.center = coordinate2D;
region.span = span;
[mapView setRegion:region];
[self.view addSubview:mapView];
_mapView = mapView;
#endif
MKMapView *mapView = [[MKMapView alloc]initWithFrame:self.view.bounds];
//中心点经纬度
CLLocationCoordinate2D coordinate2D = {39.9087607478,116.3975780499};
//比例
MKCoordinateSpan span = {0.01,0.01};
//设置范围
[mapView setRegion:MKCoordinateRegionMake(coordinate2D, span)];
[self.view addSubview:mapView];
_mapView = mapView;
}
return _mapView;
}
/**
* 添加标注
*/
- (void)addAnnotation
{
CLLocationCoordinate2D coordinate2D = {39.9087607478,116.3975780499};
//大头针对象
MKPointAnnotation *pointAnnotation = [[MKPointAnnotation alloc]init];
pointAnnotation.coordinate = coordinate2D;
//标题
pointAnnotation.title = @"北京市";
//子标题
pointAnnotation.subtitle = @"天安门";
//添加标注
[self.mapView addAnnotation:pointAnnotation];
#if 0
//1.添加一个标注
[self.mapView addAnnotation:<#(id<MKAnnotation>)#>];
//2.一次性添加多个标注
[self.mapView addAnnotations:<#(NSArray *)#>];
//3.移除指定标注
[self.mapView removeAnnotation:<#(id<MKAnnotation>)#>];
//4.移除多个标注
[self.mapView removeAnnotations:<#(NSArray *)#>];
//5.获取多个标注
self.mapView.annotations;
//6.移除所有标注
[self.mapView removeAnnotations:self.mapView.annotations;];
#endif
}
- (void)viewDidLoad {
[super viewDidLoad];
//地图显示类
MKMapView *mapView = [[MKMapView alloc]initWithFrame:self.view.bounds];
[self.view addSubview:mapView];
[self addAnnotation];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
ios-地图 大头针的使用
最新推荐文章于 2023-02-09 23:25:42 发布