您的位置:首页 > 移动开发 > IOS开发

iOS 硬件 大头针 - 基础  添加

2016-04-29 20:51 465 查看
-(void)touchesBegan: 。。。{

UITouch *touch = [touches anyObject];

CGPoint point = [touch locationInView:self.mapView];

CLLocationCoordinate2D coordinate =  [self.mapView convertPoint:point toCoordinateFromView:self.mapView];

[self addAnnotation:coordinate];


}

(void)addAnnotation:(CLLocationCoordinate2D )coordinate{

__block MyAnnotation *myAnnotation = [[MyAnnotation alloc]init];

// myAnnotation.title = @”我的位置”;

myAnnotation.coordinate = coordinate;

[self.mapView addAnnotation:myAnnotation];    ==== 他需要一个实现 <MKAnnotation>协议的对象而系统没有提供 所以要自己写一个类实现这个协议   可以自己添加


//@property (nonatomic, assign) CLLocationCoordinate2D coordinate;

//@property (nonatomic, copy, nullable) NSString *title;

//@property (nonatomic, copy, nullable) NSString *subtitle;

//
CLLocation *location = [[CLLocation alloc]initWithLatitude:coordinate.latitude longitude:coordinate.longitude];

[self.geocode reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

[placemarks enumerateObjectsUsingBlock:^(CLPlacemark * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

myAnnotation.title = obj.locality;

myAnnotation.subtitle = obj.subLocality;
}];

}];


}

//删除大头针

- (void)touchesMoved:。。。{

NSArray *annotation = [self.mapView annotations];

[self.mapView removeAnnotations:annotation];


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios 硬件