我是使用 MkCircle 声明位置现在我必须在 MKMapView(拖放)中将圆形位置更改为另一个位置。我怎么能做到这一点。任何帮助将不胜感激。
问问题
1186 次
1 回答
3
首先,我建议你有一些类属性来跟踪覆盖:
@property (nonatomic, weak) MKCircle *overlay;
然后,定义一个UIPanGestureRecognizer
来执行拖放:
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[self.mapView addGestureRecognizer:pan];
要使手势可取消,您应该导入以下标头:
#import <UIKit/UIGestureRecognizerSubclass.h>
并且您需要为此手势识别器编写一个处理程序,该处理程序为您拖动的位置添加一个覆盖并删除旧的(但如果手势没有从覆盖开始,则取消该手势):
- (void)handlePan:(UIPanGestureRecognizer *)gesture
{
static CGPoint originalPoint;
if (gesture.state == UIGestureRecognizerStateBegan) {
CGPoint point = [gesture locationInView:gesture.view];
CLLocationCoordinate2D tapCoordinate = [self.mapView convertPoint:point toCoordinateFromView:gesture.view];
CLLocation *tapLocation = [[CLLocation alloc] initWithLatitude:tapCoordinate.latitude longitude:tapCoordinate.longitude];
CLLocationCoordinate2D originalCoordinate = [self.overlay coordinate];
CLLocation *originalLocation = [[CLLocation alloc] initWithLatitude:originalCoordinate.latitude longitude:originalCoordinate.longitude];
if ([tapLocation distanceFromLocation:originalLocation] > [self.overlay radius]) {
gesture.state = UIGestureRecognizerStateCancelled;
return;
}
else
originalPoint = [self.mapView convertCoordinate:originalCoordinate toPointToView:gesture.view];
}
if (gesture.state == UIGestureRecognizerStateChanged) {
CGPoint translation = [gesture translationInView:gesture.view];
CGPoint newPoint = CGPointMake(originalPoint.x + translation.x, originalPoint.y + translation.y);
CLLocationCoordinate2D newCoordinate = [self.mapView convertPoint:newPoint toCoordinateFromView:gesture.view];
MKCircle *circle = [MKCircle circleWithCenterCoordinate:newCoordinate radius:[self.overlay radius]];
[self.mapView addOverlay:circle];
[self.mapView removeOverlay:self.overlay];
self.overlay = circle;
}
}
请注意,scrollEnabled
必须关闭地图才能识别此手势。
于 2013-08-23T15:51:56.527 回答