iOS 硬件 大头针 - 基础  添加

来源:互联网 发布:郑州软件 编辑:程序博客网 时间:2024/05/19 13:44

-(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];

}

0 0