清除地图并显示另一条路线

时间:2014-07-25 12:56:21

标签: android ios skmaps

编辑:我的SKMaps有问题。我正在尝试使用按钮显示一个菜单,并在单击按钮时显示带有计算路径的地图。当我点击它时,我下载一个gpx文件并调用路径的计算来执行它。我第一次运行它的计算工作(计算它并专注于它)。但是,每次我调用该函数时,路径确实已经计算出来,但焦点已经到目前为止。

  • 我的代码是iOS,但同样的问题也出现在Android上。
  • Myrouting代表是自己的。
  • 在我再次将其添加为子视图之前删除了mapView。
  • 显示的路线没有聚焦。

班上的相关部分是:

IOS

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self requestFiles];
    fileData = [[NSMutableData alloc] init];
    self.mapView = [[SKMapView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
    self.mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;    
    [SKRoutingService sharedInstance].mapView = self.mapView;
    [SKRoutingService sharedInstance].routingDelegate = self;
}


-(void) generateContent{
    self.container.contentSize = CGSizeMake(320, numberOfRoutes*65);
    for (int i = 0; i<numberOfRoutes; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0,i*50, 320, 55);
        [button addTarget:self action:@selector(displayRoute:) forControlEvents:UIControlEventTouchUpInside];
        [button setTag:i+1];
        [self.container addSubview:button];
    }

}


-(void)displayRouteFile:(NSString *) fname{
    if (downloadComplete) {
        for (UIView *item in self.container.subviews) {
            [item removeFromSuperview];
        }
        [self generateContent];
        downloadComplete = NO;
        self.mapView.frame = CGRectMake(0,(activeButtonNumber+1)*50+10, 320, 200);
        self.container.contentSize = CGSizeMake(320, self.container.contentSize.height+210);
        for (UIView *item in self.container.subviews) {
            if ([item isKindOfClass:[UIButton class]]) {
                UIButton *buttonToMove = (UIButton *) item;
                if (buttonToMove.tag>activeButtonNumber+1) {
                    [buttonToMove setCenter:CGPointMake(item.center.x, item.center.y+220)];
                }
            }
        }
        [[SKRoutingService sharedInstance]clearCurrentRoutes];
        [[SKRoutingService sharedInstance] clearAllRoutesFromCache];
        [[SKRoutingService sharedInstance] clearRouteAlternatives];
        [[SKRoutingService sharedInstance] startRouteFromGPXFile:fname];
        [self.container addSubview:self.mapView];
    }
}

-(IBAction)displayRoute:(UIButton *)sender{
    UIButton *button = (UIButton *)sender;
    [button setBackgroundImage:activeButtonBackground forState:UIControlStateNormal];
    int route = button.tag-1;
    activeButtonNumber = route;
    receiveFile = YES;
    //download route
    ...
    fileData = [[NSMutableData alloc] init];
}

- (void)routingService:(SKRoutingService *)routingService didFinishRouteCalculationWithInfo:(SKRouteInformation*)routeInformation{
    NSLog(@"Route is calculated with id: %u", routeInformation.routeID);
    [routingService zoomToRouteWithInsets:UIEdgeInsetsZero]; 
    // zoom to current route
    ^^^THIS PART DOES NOT WORK^^^
}

- (void)routingServiceDidFailRouteCalculation:(SKRoutingService *)routingService{
    NSLog(@"Route calculation failed.");
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[SKRoutingService sharedInstance]clearCurrentRoutes];
    activeButtonNumber = 0;
}

ANDROID:

我有一个名为TestThis的Activity,我多次打开它(每次我提供一个导致GPX文件的不同文件路径)。第一次打开Activity时整个过程运行正常 - 无论我提供什么文件路径,它都会显示路径,然后放大它。如果我返回并选择另一个文件路径来创建活动,则会显示新路线,但地图会缩放到其他位置。

这就是事情发生的顺序:

  • onCreate我从Bundle获取文件的路径。
  • onSurfaceCreated我清除currentRoute并开始计算 新的。
  • onRouteCalculationCompleted我将新路由设置为当前路由。
  • onAllRoutesCompleted我尝试缩放。

公共类TestThis扩展Activity实现SKRouteListener,SKMapSurfaceListener {

private static SKMapSurfaceView mapView;
String path;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.prazen);

    Bundle bundle = getIntent().getExtras(); 
    path = bundle.getString("filepath");

    RelativeLayout theArea = (RelativeLayout) findViewById(R.id.placeHere);
    SKMapViewHolder myMapHolder = new SKMapViewHolder(TestThis.this);       
    mapView = myMapHolder.getMapSurfaceView();
    mapView.setMapSurfaceListener(this);
    mapView.getMapSettings().setCurrentPositionShown(false);
    theArea.addView(myMapHolder);

    SKRouteManager.getInstance().setRouteListener(TestThis.this);               
}

@Override
public void onRouteCalculationCompleted(final int statusMessage, final int routeDistance, final int routeEta,  final boolean thisRouteIsComplete, final int id) {

    //SKRouteManager.getInstance().zoomToRoute((float)1.2, (float)1.2, 110, 8, 8, 8);
    if(SKRouteManager.getInstance().setCurrentRouteByUniqueId(id)){
        System.out.println("Current route selected!");          
    }

}

@Override
public void onAllRoutesCompleted() { 
    System.out.println("On all routes compl");
    SKRouteManager.getInstance().zoomToRoute((float)1.2, (float)1.2, 110, 8, 8, 8);
    //SKRouteManager.getInstance().zoomMapToCurrentRoute();
}

@Override
public void onSurfaceCreated() {
    // TODO Auto-generated method stub
    SKRouteManager.getInstance().clearCurrentRoute();
    SKRouteManager.getInstance().setRouteFromGPXFile(path, SKRouteSettings.SKROUTE_CAR_SHORTEST, false, false, false);      
}

}

1 个答案:

答案 0 :(得分:1)

<强> IOS 该错误在客户端。

问题在于,当计算路线时,使用地图视图的前一帧,这就是路线不居中的原因。移动  

之前的[self.container addSubview:self.mapView];
[[SKRoutingService sharedInstance] clearCurrentRoutes]; 
[[SKRoutingService sharedInstance] clearAllRoutesFromCache]; 
[[SKRoutingService sharedInstance] clearRouteAlternatives]; 
[[SKRoutingService sharedInstance] startRouteFromGPXFile:fname]; 

应该解决问题。

<强>机器人: 在创建活动后立即调用zoomToRoute()时,可能会出现缩放不正确的问题。作为该问题的解决方法,可以遵循与v2.2开源演示相同的方法:避免活动重新创建并从与显示地图的活动相同的活动中选择GPX轨道 - 请参阅中的“轨道”选项演示菜单。

原始错误/原因将在未来的更新中解决。