在我的一个应用中,我需要在2个RMPointAnnotation之间画一条线。 我所做的就是在viewDidAppear中创建所有内容:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
RMPointAnnotation *annotation = [[RMPointAnnotation alloc] initWithMapView:self.mapView
coordinate:self.mapView.centerCoordinate
andTitle:@"annotation"];
[self.mapView addAnnotation:annotation];
RMPointAnnotation *annotation2 = [[RMPointAnnotation alloc] initWithMapView:self.mapView
coordinate:CLLocationCoordinate2DMake(self.mapView.centerCoordinate.latitude - 0.005, self.mapView.centerCoordinate.longitude + 0.005)
andTitle:@"annotation 2"];
[self.mapView addAnnotation:annotation2];
RMAnnotation *lineAnnotation = [[RMAnnotation alloc] initWithMapView:self.mapView coordinate:annotation.coordinate andTitle:nil];
[self.mapView addAnnotation:lineAnnotation];
}
然后我实现了mapView:layerForAnnotation:
- (RMMapLayer *)mapView:(RMMapView *)mapView layerForAnnotation:(RMAnnotation *)annotation
{
if (annotation.isUserLocationAnnotation)
return nil;
RMShape *shape = [[RMShape alloc] initWithView:mapView];
shape.lineColor = [UIColor orangeColor];
shape.lineWidth = 5.0;
for (RMAnnotation *annotation in (NSArray *)mapView.annotations) {
if ([annotation isKindOfClass:[RMPointAnnotation class]]) {
[self.shape addLineToCoordinate:annotation.coordinate];
}
}
return self.shape;
}
但是因为我的RMPointAnnotation都是可拖动的,所以我想在拖动时不断重绘2个注释之间的界限。
如何使用MapBox实现这样的功能?
由于
答案 0 :(得分:0)
因此,解决此问题的一种方法是创建自己的自定义视图,以跟上您的上一个位置。
@implementation CMLPointView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.userInteractionEnabled = YES;
self.layer.cornerRadius = frame.size.width/2;
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
self.lastLocation = self.center;
}
您可以为坐标所需的每个点创建一个视图。我在视图控制器中运行此循环,以便我可以从那里处理平移手势。
for (CLLocation *loc in self.poiCoordinates) {
CMLPointView *point = [[CMLPointView alloc] initWithFrame:CGRectMake([self.mapView coordinateToPixel:loc.coordinate].x-10.0f, [self.mapView coordinateToPixel:loc.coordinate].y-10.0f, 20.0f, 20.0f)];
point.backgroundColor = [UIColor purpleColor];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[point addGestureRecognizer:panRecognizer];
[self.view addSubview:point];
}
为了实际处理平移,我每次创建一个新的RMAnnotation并为该注释绘制RMS形状。
- (void)handlePan: (UIPanGestureRecognizer *) pan
{
CMLPointView *pointView = (CMLPointView *)pan.view;
CGPoint translation = [pan translationInView:self.view];
pointView.center = CGPointMake(pointView.lastLocation.x + translation.x,
pointView.lastLocation.y + translation.y);
// Just trying to remove the annotation for RMShape
[self.mapView removeAnnotation:[self.mapView.annotations lastObject]];
RMAnnotation *annotation = [[RMAnnotation alloc] initWithMapView:self.mapView coordinate:[self.mapView pixelToCoordinate:pointView.center] andTitle:@"POI"];
[self.poiCoordinates removeObjectAtIndex:0];
[self.poiCoordinates insertObject:[[CLLocation alloc] initWithLatitude:[self.mapView pixelToCoordinate:pointView.center].latitude longitude:[self.mapView pixelToCoordinate:pointView.center].longitude] atIndex:0];
annotation.userInfo = self.poiCoordinates;
[annotation setBoundingBoxFromLocations:self.poiCoordinates];
[self.mapView addAnnotation:annotation];
}
希望这有帮助!如果您对我做某事的原因有疑问,请发表评论。