Skip to content Skip to sidebar Skip to footer

Zoom HERE Maps To Show All Markers Android

I have multiple locations that I am displaying on HERE maps in my Android app. There are in all 4 markers, out of which 3 markers are in one city and 1 is in another city. Currentl

Solution 1:

Same thing I achieved using this.

val geoCoordinateList = ArrayList<GeoCoordinate>()
val geoBoundingBox = GeoBoundingBox.getBoundingBoxContainingGeoCoordinates(geoCoordinateList)
map.zoomTo(geoBoundingBox, Map.Animation.LINEAR, Map.MOVE_PRESERVE_TILT)

enter image description here


Solution 2:

You have to use the CameraUpdate class to do (probably) all programmatic map movements.

To do this, first calculate the bounds of all the markers like below:

LatLngBounds.Builder builder = new LatLngBounds.Builder();
//All Marker List to get Position
for (Marker marker : markers) {
 builder.include(marker.getPosition());
}
//Create Latlong Bounds.
LatLngBounds bounds = builder.build();

Then obtain a movement description object by using the factory: CameraUpdateFactory lime below :

int padding = 0; // offset from edges of the map in pixels


CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);

Now we can use that in Map like below:

googleMap.moveCamera(cu);

Solution 3:

You can use LatLngBounds.Builder() to make multiple markers to viewable under map.

It has a method include, which allow you to add multiple LAT LNG locations.

val bounds: LatLngBounds = locatebuilder.build() // locatebuilder is LatLngBounds.Builder
val padding = 200 // offset from edges of the map in pixels
val cu = CameraUpdateFactory.newLatLngBounds(bounds, padding)
mMap!!.animateCamera(cu)

EDIT

Make a new list of markers when you're adding it inside map

for (mark in markers) { // markers is a list of markers added in the map
    locatebuilder.include(mark.position)
}

Solution 4:

According this issue: https://github.com/heremaps/here-android-sdk-examples/issues/176 you need to zoom like this:

private void navigateToMapMarkers(List<MapMarker> markers, int padding) {
        // find max and min latitudes and longitudes in order to calculate
        // geo bounding box so then we can map.zoomTo(geoBox, ...) to it.
        double minLat = 90.0d;
        double minLon = 180.0d;
        double maxLat = -90.0d;
        double maxLon = -180.0d;

        for (MapMarker marker : markers) {
            GeoCoordinate coordinate = marker.getCoordinate();
            double latitude = coordinate.getLatitude();
            double longitude = coordinate.getLongitude();
            minLat = Math.min(minLat, latitude);
            minLon = Math.min(minLon, longitude);
            maxLat = Math.max(maxLat, latitude);
            maxLon = Math.max(maxLon, longitude);
        }

        GeoBoundingBox box = new GeoBoundingBox(new GeoCoordinate(maxLat, minLon),
                new GeoCoordinate(minLat, maxLon));

        ViewRect viewRect = new ViewRect(padding, padding, m_map.getWidth() - padding * 2,
                m_map.getHeight() - padding * 2);
        m_map.zoomTo(box, viewRect, Map.Animation.LINEAR, Map.MOVE_PRESERVE_ORIENTATION);
}

Solution 5:

You need to set the padding on the map before you apply the zoom

// (left, Top, right, Bottom)
map.setPadding(100, 100, 800, 400)

then you create the GeoBoundingBox and implement the zoom, or if you are getting a calculated route then you can get it in this way:

routeResult.route.boundingBox?.let { boundingBox ->
      map.zoomTo(boundingBox, Map.Animation.BOW, Map.MOVE_PRESERVE_ORIENTATION)
}

I'm using HERE maps 3.18.2 version


Post a Comment for "Zoom HERE Maps To Show All Markers Android"