here-android-sdk-examples icon indicating copy to clipboard operation
here-android-sdk-examples copied to clipboard

How to zoom in and out the map maintaining the course to destination?

Open binod-techindustan opened this issue 4 years ago • 9 comments

Here is the current implementation of zoom in and out of the map during navigation.

ivZoomIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NavigationManager.getInstance().setMapUpdateMode(NavigationManager.MapUpdateMode.NONE);
                m_map.setZoomLevel(m_map.getZoomLevel() + 0.5, Map.Animation.BOW);
                tvReCenter.setVisibility(View.VISIBLE);
            }
        });

        ivZoomOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NavigationManager.getInstance().setMapUpdateMode(NavigationManager.MapUpdateMode.NONE);
                m_map.setZoomLevel(m_map.getZoomLevel() - 0.5, Map.Animation.BOW);
                tvReCenter.setVisibility(View.VISIBLE);
                tvReCenter.setVisibility(View.VISIBLE);
            }
        });
        tvReCenter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                m_navigationManager.setMapUpdateMode(NavigationManager.MapUpdateMode.ROADVIEW);
                GeoPosition lkp = PositioningManager.getInstance().getLastKnownPosition();
                if (lkp != null && lkp.isValid())
                    m_map.setCenter(lkp.getCoordinate(), Map.Animation.BOW);
                tvReCenter.setVisibility(View.GONE);
                m_map.setZoomLevel(17.0f, Map.Animation.BOW);
            }
        });

With current implementation when we zoom in or out the map, the map remains still at the current position where it was zoomed and the position indication icon goes out of the map course to the destination. Actually we want to zoom in/out the map maintaining map updates and position indicator update. The reason of not updating the map is due to NavigationManager.getInstance().setMapUpdateMode(NavigationManager.MapUpdateMode.NONE); but the problem is if we don't this, the zoom in/out out the map doesn't work.

If we zoom the map with the below code it didn't zoom.

ivZoomOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                m_map.setZoomLevel(m_map.getZoomLevel() - 0.5, Map.Animation.BOW);

            }
        });

binod-techindustan avatar Mar 06 '20 06:03 binod-techindustan

@starand can you please help?

binod-techindustan avatar Mar 06 '20 08:03 binod-techindustan

Hi @binod-techindustan Did you try using RoadView class to zoom in or out during navigation?

E.g. instead of this: m_map.setZoomLevel(m_map.getZoomLevel() - 0.5, Map.Animation.BOW); call this: NavigationManager.getInstance().getRoadView().zoomOut();

NazarKacharaba avatar Mar 06 '20 15:03 NazarKacharaba

@binod-techindustan did you try this approach?

NazarKacharaba avatar Mar 23 '20 08:03 NazarKacharaba

@NazarKacharaba tried it but didn't worked as expected.

binod-techindustan avatar Mar 23 '20 09:03 binod-techindustan

Could you tell what exactly did not work for you? There is another more hacky way to achieve what you need, try this snippet:

final Handler handler = new Handler(Looper.getMainLooper());
void zoomInOut(boolean zoomIn) {
    handler.removeCallbacksAndMessages(null);
    final NavigationManager navigationManager = NavigationManager.getInstance();
    // stop any map movements to set zoom level
    navigationManager.setMapUpdateMode(MapUpdateMode.NONE);
    map.setZoomLevel(map.getZoomLevel() + (zoomIn ? 0.5 : -0.5), Map.Animation.LINEAR);
    // wait until zoom is set
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            navigationManager.setMapUpdateMode(MapUpdateMode.POSITION_ANIMATION);
        }
    }, 2000/*time to wait until LINEAR zoom animation is done*/);
}

NazarKacharaba avatar Mar 24 '20 08:03 NazarKacharaba

@NazarKacharaba with NavigationManager.getInstance().getRoadView().zoomOut(); it didn't do anything in the map. I will try the new one.

binod-techindustan avatar Mar 25 '20 05:03 binod-techindustan

Thanks @NazarKacharaba the last one works fine

binod-techindustan avatar Mar 25 '20 11:03 binod-techindustan

@NazarKacharaba after zoom in/out with this the position indicator icon began to flicker. It's like jumping the position indicator. But after recenter it begin to work smoothly. Here is recenter code:

 tvReCenter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) { 
m_navigationManager.setMapUpdateMode(NavigationManager.MapUpdateMode.ROADVIEW);
                GeoPosition lkp = PositioningManager.getInstance().getLastKnownPosition();
                if (lkp != null && lkp.isValid())
                    m_map.setCenter(lkp.getCoordinate(), Map.Animation.BOW);
                tvReCenter.setVisibility(View.GONE);
                m_map.setZoomLevel(17.0f, Map.Animation.BOW);
});

So can we zoom the map level with map update mode NavigationManager.MapUpdateMode.ROADVIEW?

binod-techindustan avatar Mar 27 '20 14:03 binod-techindustan

@binod-techindustan In NavigationManager.MapUpdateMode.ROADVIEW mode you should use NavigationManager.getInstance().getRoadView().zoomOut() or NavigationManager.getInstance().getRoadView().zoomIn() methods.

NazarKacharaba avatar Apr 01 '20 12:04 NazarKacharaba