大头针
2017-03-25 本文已影响15人
陈水寒
需要自定义大头针模型,并且遵循<MKAnnotation>协议,并且需要包含以下几个属性:
// 大头针的坐标
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
// 标题和子标题
@property (nonatomic, copy, nullable) NSString *title;
@property (nonatomic, copy, nullable) NSString *subtitle;
下面实现点击地图,在点击位置添加一个大头针
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// 获取点击
CGPoint point = [[touches anyObject] locationInView:self.mapView];
// 屏幕点击的点转换成地图坐标
CLLocationCoordinate2D coordinate = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
// GGAnnotation为自定义大头针模型
GGAnnotation *annotation = [[GGAnnotation alloc] init];
annotation.coordinate = coordinate;
// 添加大头针
[self.mapView addAnnotation:annotation];
}
演示效果:
点击屏幕对应位置添加大头针.gif