2017-09-13 86 views
0

我试图将MapRoute对象添加到地图时,似乎在某些较旧的设备上出现内存不足错误。通常发生在城市规模之外的路线,特别是美国境内的非常长的路线。我尝试过模拟器设备(Nexus 4.4.4)和物理设备(Samsung Galaxy Prevail 4.4.4)。这里是我的计算路线代码:Here map api Android:MapRoute zoomTo()内存不足错误()

public void calculateRoute(GeoCoordinate start, GeoCoordinate end, final RouteListener listener) { 
     RoutePlan routePlan = new RoutePlan(); 
     RouteOptions routeOptions = new RouteOptions(); 
     routeOptions.setTransportMode(RouteOptions.TransportMode.CAR); 
     routeOptions.setRouteType(RouteOptions.Type.SHORTEST); 
     routeOptions.setRouteCount(1); 
     routePlan.setRouteOptions(routeOptions); 

     RouteManager routeManager = new RouteManager(); 
     routePlan.addWaypoint(start); 
     routePlan.addWaypoint(end); 

     routeManager.calculateRoute(routePlan, new RouteManager.Listener() { 
      @Override 
      public void onCalculateRouteFinished(RouteManager.Error error, List<RouteResult> list) { 
       if(error == RouteManager.Error.NONE) { 
        if(list.get(0).getRoute() != null) { 
         MapRoute mapRoute = new MapRoute(list.get(0).getRoute()); 
         listener.gotRoute(mapRoute); 
        } else { 
         listener.gotRoute(null); 
        } 
       } else { 
        listener.gotRoute(null); 
       } 
      } 
      @Override 
      public void onProgress(int i) {} 
     }); 

    } 

这里是我的片段回调的结果:

 PlaceServices placeServices = new PlaceServices(getActivity()); 
       placeServices.calculateRoute(pickupMarker.getCoordinate(), dropoffMarker.getCoordinate(), 
         new PlaceServices.RouteListener() { 
          @Override 
          public void gotRoute(MapRoute mapRoute) { 
           if(mapRoute != null) { 

            map.addMapObject(mapRoute); 
            GeoBoundingBox gbb = mapRoute.getRoute().getBoundingBox(); 
// Here is where I get an OOM 
            map.zoomTo(gbb, Map.Animation.NONE, Map.MOVE_PRESERVE_ORIENTATION); 
           } 
          } 
         }); 

人遇到这个问题?它不会影响较新的设备,我已经在Nexus 6上进行了测试,没有任何问题。

堆栈跟踪:

Error in converting images 
    java.lang.OutOfMemoryError 
     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 
     at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:620) 
     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596) 
     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:634) 
     at com.here.android.mpa.internal.x.a(DiskCache.java:447) 
     at com.here.android.mpa.internal.x$b.a(DiskCache.java:399) 
     at com.here.android.mpa.internal.x$b.doInBackground(DiskCache.java:381) 
     at android.os.AsyncTask$2.call(AsyncTask.java:288) 
     at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
     at java.lang.Thread.run(Thread.java:841) 

更新:我花了一分钟,以缩小下来,因为这个问题是零星的,但在zoomTo出现错误,不添加对象到地图中。我更新了标题和评论。

回答

0

我最终调用zoomTo()方法,我计算了其解决了该问题的途径之前,想这不是很好的尝试添加路由对象和缩放在同一时间:

// Zoom to markers before the route is calculated to avoid OOM on some devices. 
     if(pickupCoordinate != null && dropoffCoordinate != null) { 
      GeoBoundingBox geoBoundingBox; 
      geoBoundingBox = Util.calculateBoundingBox(pickupCoordinate, dropoffCoordinate); 
      map.zoomTo(geoBoundingBox, Map.Animation.LINEAR, Map.MOVE_PRESERVE_ORIENTATION); 
     } 

     PlaceServices placeServices = new PlaceServices(getActivity()); 
     placeServices.calculateRoute(pickupMarker.getCoordinate(), dropoffMarker.getCoordinate(), 
       new PlaceServices.RouteListener() { 
        @Override 
        public void gotRoute(MapRoute mapRoute) { 
         if(mapRoute != null) { 
          map.addMapObject(mapRoute);  
         } 
        } 
       });