HERE maps - Move map position

86 views Asked by At

I would like to do very simple thing which is moving HERE map left / right / up / down. Let's focus on one thing, so moving map up. This works fine when dragging the map but how to do that from code?

I've tried taking current GeoBox modifying latitude only (keeping longitude unchanged) and then using:

camera.lookAt(myNewBox, new GeoOrientationUpdate(null, (double) 0));

It worked but I guess that due to spherical coordinate system map not only moved up but also to the right (of course it depends on location). Anyway, I would only like to move the map in one direction, preferably by just giving it value in px / dp. Is it possible? Or can I maybe simulate drag from code?

2

There are 2 answers

0
LLL On BEST ANSWER

I was finally able to achieve what I needed. Initial approach with moving whole box:

camera.lookAt(myNewBox, new GeoOrientationUpdate(null, (double) 0));

resulted in map zooming out a bit (due to spherical coordinate system) because map wanted to show new corners and since earth is not flat it had to zoom out it my case. Probably in case of different coordinates it could be zooming in as well.

In order to achieve this without zoom effect I've used different variant of lookAt function which shows single point only:

camera.lookAt(new GeoCoordinates(centerPointLatitude + shift, centerPointLongitude));

It required computing center point first but it does the job without any zoom or left / right shift.

2
igomez On

to programmatically move the map in a specific direction (e.g., up) without affecting the longitude, you can achieve this by updating the GeoBox of the map. Below is a solution in Android using the HERE Mobile SDK:

import com.here.sdk.core.GeoBox;
import com.here.sdk.core.GeoCoordinates;
import com.here.sdk.core.GeoOrientationUpdate;
import com.here.sdk.mapviewlite.Camera;
import com.here.sdk.mapviewlite.MapViewLite;

public class MapMovementHelper {

    private final MapViewLite mapView;

    public MapMovementHelper(MapViewLite mapView) {
        this.mapView = mapView;
    }

    public void moveMapUp(double distanceInMeters) {
        Camera camera = mapView.getCamera();

        // Get the current visible region (GeoBox) of the map.
        GeoBox currentGeoBox = mapView.getBoundingBox();

        // Calculate the new latitude for moving the map up.
        double newLatitude = currentGeoBox.getTopLeft().latitude + distanceInMeters / mapView.getPixelScaleAt(0);

        // Create a new GeoBox with the updated latitude.
        GeoBox newGeoBox = new GeoBox(
                new GeoCoordinates(newLatitude, currentGeoBox.getTopLeft().longitude),
                currentGeoBox.getBottomRight());

        // Move the camera to the new GeoBox.
        camera.lookAt(newGeoBox, new GeoOrientationUpdate(null, (double) 0));
    }
}

This code defines a MapMovementHelper class that provides a method moveMapUp(double distanceInMeters) to move the map up by a specified distance in meters. The method calculates the new latitude based on the current visible region and updates the GeoBox accordingly.

You can use this helper class in your activity or fragment:

MapMovementHelper mapMovementHelper = new MapMovementHelper(mapView);
mapMovementHelper.moveMapUp(1000); // Move the map up by 1000 meters.

Adjust the distanceInMeters parameter as needed for your specific use case.