地图的显示以及如何添加大头针

来源:互联网 发布:远程教育软件k6 编辑:程序博客网 时间:2024/06/15 02:43

地图的显示

 1.导入系统的包#import <MapKit/MapKit.h>

 2.定义地图MKMapView *_mapView;

 3.创建地图视图对象_mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 20, 375, 670 - 20)];

 4.设置类型 _mapView.mapType = MKMapTypeStandard;

 5.设置代理_mapView.delegate = self;MKMapViewDelegate

 6.设置中心点和范围CLLocationCoordinate2D center = CLLocationCoordinate2DMake(39.9023, 116.3902);

 7.放大比例MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01);

 8.地图显示区域MKCoordinateRegion  region = MKCoordinateRegionMake(center, span);

 [_mapView setRegion:region animated:YES];

 9.设置地图定位位置_mapView.showsUserLocation = YES;

 10.加载到视图上[self.view addSubview:_mapView];

 以及定位如上篇

 添加大头针效果

 11.添加一个长按手势添加大头针

 UILongPressGestureRecognizer *g = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];

 12.将手势加到地图上

 [_mapView addGestureRecognizer:g];

 13.触法方法

 - (void)longPressAction:(UIGestureRecognizer *)g

 {

 获取坐标

 CGPoint point = [g locationInView:_mapView];

 坐标转换成经纬度

 CLLocationCoordinate2D coor = [_mapView convertPoint:point toCoordinateFromView:_mapView];

 

 添加大头针

 MKPointAnnotation *anno = [[MKPointAnnotation alloc] init];

 设置标题

 anno.title = @"LH";

 anno.subtitle = @"PP";

 设置经纬度

 anno.coordinate = coor;

 

  添加到地图上

 [_mapView addAnnotation:anno];

 

 }

0 0