编辑:我对 SKMaps 有疑问。我试图有一个带有按钮的菜单,并在单击按钮时在按钮下方显示带有计算路线的地图。当我点击它时,我会下载一个 gpx 文件并调用它来执行路由计算。我第一次运行它的计算(计算它并专注于它)。但是,每次我下次调用该函数时,确实会计算路线,但焦点已远。
- 我的代码是 iOS,但同样的问题也出现在 android 上。
- Myrouting 委托设置为 self。
- 在再次将其添加为子视图之前,我删除了 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;
}
安卓:
我有一个名为 TestThis 的活动,我多次打开它(每次我为它提供一个不同的文件路径,指向一个 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);
}
}