0

first of all, I want to apologize if I cannot make myself clear.

I have a problem using the HERE Maps SDK. I am not able to make my code work inside the onCreate statement, but outside the init() method for the map.

However, if I put the last block of code inside the init() and the end of the if-statement, everything works fine. I'm just wondering if I'm supposed to handle everything inside init(). That would get quite messy/crowded, I assume...

Here is my code:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Search for the map fragment to finish setup by calling init()
    mapFragment = (MapFragment)getFragmentManager().findFragmentById(R.id.mapfragment);
    mapFragment.init(new OnEngineInitListener() {
        @Override
        public void onEngineInitializationCompleted(OnEngineInitListener.Error error) {
            if (error == OnEngineInitListener.Error.NONE) {
                // retrieve a reference of the map from the map fragment
                map = mapFragment.getMap();
                // Set the map center to the Berlin region
                map.setCenter(new GeoCoordinate(52.520413, 13.405218, 0.0), Map.Animation.LINEAR);
                // Set the zoom level to the average between min and max
                map.setZoomLevel((map.getMaxZoomLevel() + map.getMinZoomLevel()) / 1.5);

                map.getMapTransitLayer().setMode(MapTransitLayer.Mode.EVERYTHING);

                mapFragment.getMapGesture().addOnGestureListener(new MyOnGestureListener());

                posManager = PositioningManager.getInstance();

                // Register positioning listener
                posManager.addListener(new WeakReference<>(positionListener));

                posManager.start(PositioningManager.LocationMethod.GPS_NETWORK);
                posManager.getPosition();

                map.getPositionIndicator().setVisible(true);

            } else {
                System.out.println("ERROR: Cannot initialize Map Fragment");
            }
        }

    });

    RouteManager rm = new RouteManager();

    RoutePlan routePlan = new RoutePlan();
    routePlan.addWaypoint(new GeoCoordinate(posManager.getLastKnownPosition().getCoordinate()));
    routePlan.addWaypoint(new GeoCoordinate(52.520413, 13.405218));

    // Create the RouteOptions and set its transport mode & routing type
    RouteOptions routeOptions = new RouteOptions();
    routeOptions.setTransportMode(RouteOptions.TransportMode.CAR);
    routeOptions.setRouteType(RouteOptions.Type.FASTEST);

    routePlan.setRouteOptions(routeOptions);

    // Calculate the route
    rm.calculateRoute(routePlan, new RouteListener());


}

After the semicolon of the init() statement, in the line where it says RouteManager rm = new RouteManager(); I get the following error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.here.android.tutorial.basicmapsolution/com.here.android.tutorial.basicmapsolution.BasicMapActivity}: com.here.android.mpa.common.UnintializedMapEngineException: Cannot created HERE SDK objects before MapEngine is initialized.  See MapEngine.init()
Caused by: com.here.android.mpa.common.UnintializedMapEngineException: Cannot created HERE SDK objects before MapEngine is initialized.  See MapEngine.init()
VollNoob
  • 267
  • 4
  • 15
  • 1
    MapEngine.init() is async method. So you need to wait for initialization. Then you can 'RouteManager' – onur taskin Dec 29 '15 at 15:12
  • Oh, thanks, I didn't knew that. How did you find out? And is there another way around? I have only once used an Async Task (I'm not very experienced with Android programming), but that was not as messy, because there was onPostExecute() for example. And I don't understand, why the entries in the init() are not handled before the rest of the onCreate() – VollNoob Dec 30 '15 at 12:32
  • 2
    Here is the document; https://developer.here.com/mobile-sdks/documentation/android/topics_api_nlp/com-here-android-mpa-common-mapengine.html#topic-apiref__init-context-onengineinitlistener .You can think OnEngineInitListener as AsyncTask and onEngineInitializationCompleted is like equal to onPostExecute() method. These async methods take long time to finish and work on another thread or else main thread will be blocked. – onur taskin Dec 30 '15 at 12:45
  • Ok, thank you very much! But do I have to squeeze everything into the onEngineInitializationCompleted() ? I'm sorry for my noob question, but as far as I understood I can only interact with the map, after the init(), right? Therefore, everything that is not in the onEngineInitializationCompleted() will crash the app, because the map is not yet initialized. Am I supposed to put all my code in the onEngineInitializationCompleted() ? – VollNoob Dec 30 '15 at 13:17
  • How do I know, when that task is finished? – VollNoob Jan 05 '16 at 13:21
  • 'onEngineInitializationCompleted' will be triggred when initialization complete – onur taskin Jan 05 '16 at 16:00
  • So, onEngingeInitializationCompleted() equals the onPostExecute() of an asynctask, is that correct? – VollNoob Jan 06 '16 at 12:51
  • Is there a way to access the other functions of this init() similar to the other functions of an asynctask? – VollNoob Jan 07 '16 at 12:47

1 Answers1

0

In general any HERE Mobile SDK related function can only be used after the map has been initialized successfully, i.e Once a callback in onEngineInitializationCompleted is received, there is no way to use the SDK function until the init has been completed.

From code maintainability perspective , one can always move the code into a separate modular function and call the function once onEngineInitializationCompleted provides a callback

Example, for the shared code the Route related operation can be moved into a function

private void calculateRoute(){
   RouteManager rm = new RouteManager();

    RoutePlan routePlan = new RoutePlan();
    routePlan.addWaypoint(new GeoCoordinate(posManager.getLastKnownPosition().getCoordinate()));
    routePlan.addWaypoint(new GeoCoordinate(52.520413, 13.405218));

    // Create the RouteOptions and set its transport mode & routing type
    RouteOptions routeOptions = new RouteOptions();
    routeOptions.setTransportMode(RouteOptions.TransportMode.CAR);
    routeOptions.setRouteType(RouteOptions.Type.FASTEST);

    routePlan.setRouteOptions(routeOptions);

    // Calculate the route
    rm.calculateRoute(routePlan, new RouteListener());
}

and then call this function in the callback

mapFragment.init(new OnEngineInitListener() {
    @Override
    public void onEngineInitializationCompleted(OnEngineInitListener.Error error) {
        if (error == OnEngineInitListener.Error.NONE) {
             .... 
         // call the private function 
         calculateRoute();

        } else {
            System.out.println("ERROR: Cannot initialize Map Fragment");
        }
    }

});
Dharman
  • 30,962
  • 25
  • 85
  • 135