Search code examples
androidiosskmaps

Clear map and display another route


EDIT: I have a problem with SKMaps. I am trying to have a menu with buttons and display map with a calculated route underneath a button when the it is clicked. When I click it I download a gpx file and call the calculation of route with it to be performed. The first time I run the calculation it works (both calculates it and focuses on it). However every next time I call the function the route is indeed calculated but the focus is off by far.

  • My code is iOS but the same issue is present on android as well.
  • Myrouting delegate is set to self.
  • I remove the mapView before I add it as subview again.
  • The route is displayed just not focused on.

Relevant parts of my class are:

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:

I have an Activity called TestThis which I open multiple times (every time I supply it with a different filepath leading to a GPX file). The whole process works fine the first time I open the Activity - whatever filepath I supply it with it displays the route and then zooms on it. If I go back and select another filepath to create the activity with the new route is displayed but the map zooms somewhere else.

That is the order things happen:

  • onCreate I get the path to the file from the Bundle.
  • onSurfaceCreated I clear the currentRoute and start the calculation of the new one.
  • onRouteCalculationCompleted I set the new route to be the current route.
  • onAllRoutesCompleted I try to zoom.

public class TestThis extends Activity implements 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);      
}

}

Solution

  • IOS The bug is on client side.

    The problem is that when the route is calculated the previous frame of the map view is used and that's why the route is not centred. Moving [self.container addSubview:self.mapView]; before the

    [[SKRoutingService sharedInstance] clearCurrentRoutes]; 
    [[SKRoutingService sharedInstance] clearAllRoutesFromCache]; 
    [[SKRoutingService sharedInstance] clearRouteAlternatives]; 
    [[SKRoutingService sharedInstance] startRouteFromGPXFile:fname]; 
    

    should fix the problem.

    Android: The problem with incorrect zooming might occur when the call to zoomToRoute() is made soon after creating the activity. As a workaround for the issue the same approach as in the v2.2 open source demo may be followed: avoiding activity recreation and selecting the GPX tracks from the same activity as the one that displays the map - see the "Tracks" option in the demo's menu.

    The original bug/cause will be addressed in a future update.