【问题标题】:How can I retain a NMARoute reference after calling navigationManager.stop?如何在调用 navigationManager.stop 后保留 NMARoute 引用?
【发布时间】:2018-01-17 16:54:32
【问题描述】:

我正在 iOS 应用中实现 Here Maps 导航 SDK。 当我在开始导航之前计算路线时,我将路线保存在一个实例变量中:self.route

// Trigger the route calculation
[self.router
    calculateRouteWithStops:stops
                routingMode:routingMode
            completionBlock:^(NMARouteResult* routeResult, NMARoutingError error) {
              if (error) {
                  reject(@"route_calculation_error", [NSString stringWithFormat:@"Error %d when calculating route", (int)error], nil);
                  return;
              }
              if (!routeResult || routeResult.routes.count == 0) {
                  reject(@"route_calculation_no_result", @"Error when calculation route: no result", nil);
                  return;
              }

              // Let's add the 1st result onto the map
              self.route = routeResult.routes[0];
              self.mapRoute = [NMAMapRoute mapRouteWithRoute:self.route];
              [self.mapView addMapObject:self.mapRoute];

              [self zoomToRoute];

              [self updateNavigationMode:kNavigationModeRoute];
              resolve(@{
                  @"length" : @(self.route.length),
                  @"duration" : @(self.route.tta.duration),
              });
            }];

然后当我调用停止导航的函数时,路线将从地图中删除,self.route 为零。我想在地图上保留路线并返回行程视图。

[self zoomToRoute];
[self.navigationManager stop]; // after this call, self.route == nil
[self updateNavigationMode:kNavigationModeRoute];

这是我想出的解决方案,但我宁愿不重新创建 mapRoute,感觉很hacky...

[self zoomToRoute];
// for some reason navigationManger#stop releases self.route and removes
// the mapRoute from the map so we create it again
NMARoute* route = self.route;
[self.navigationManager stop];
self.route = route;
self.mapRoute = [NMAMapRoute mapRouteWithRoute:self.route];
[self.mapView addMapObject:self.mapRoute];
[self updateNavigationMode:kNavigationModeRoute];

【问题讨论】:

    标签: objective-c here-api


    【解决方案1】:

    NMARoute 的所有权独立于导航和所有其他上下文。只要您的 self.route 保留一个强引用,您就可以在您的应用程序需要时重复使用 NMARoute 对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-28
      • 2019-11-22
      相关资源
      最近更新 更多