Zoom HERE maps to show all markers Android

心不动则不痛 提交于 2021-01-29 07:40:50

问题


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. Currently, my map zooms to a level where only the 3 markers in one city are visible, whereas the 4th one is not visible. In order to see that marker, I have to zoom out a considerable level.

Is there a way, I can show all the markers within the bounds of the map?

Here is my code:-

 m_map = mapFragment.getMap();
                m_map.setZoomLevel((m_map.getMaxZoomLevel() + m_map.getMinZoomLevel()) / 2);
                m_map.setMapScheme(Map.Scheme.NORMAL_DAY);

  final GeoCoordinate finalZoomLatLng = m_map.getCenter();
        new Handler().postDelayed(() -> {
            m_map.setCenter(finalZoomLatLng, Map.Animation.NONE);
            m_map.zoomTo(m_map.getBoundingBox(), Map.Animation.NONE, Map.MOVE_PRESERVE_ORIENTATION);
        }, 1000); 

EDIT

With the help of Rahul's answer I have achieved something like so:-

for (int i = 0; i < tasks.size(); i++)
{
    final GeoCoordinate latLng = m_getPosition(tasks[i]);
    LatLng mLatLng = new LatLng(latLng.getLatitude(), latLng.getLongitude());
    mLatLngBounds.include(mLatLng);
}

GeoCoordinate finalZoomLatLng = new GeoCoordinate(mLatLngBounds.build().getCenter().latitude, mLatLngBounds.build().getCenter().longitude);
            new Handler().postDelayed(() -> {
                m_map.setCenter(finalZoomLatLng, Map.Animation.NONE);
                m_map.zoomTo(new GeoBoundingBox(finalZoomLatLng, 0.0f, 200000.0f), Map.Animation.NONE, Map.MOVE_PRESERVE_ORIENTATION);
            }, 1000);

Now with this, the map is zoomed in the center of the map with all the locations around it. However, there is no marker at this location, so I have to zoom out a bit to see all markers. How can I solve this?

Any help is highly appreciated.


回答1:


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);



回答2:


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)
}



回答3:


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)




回答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);
}


来源:https://stackoverflow.com/questions/58056090/zoom-here-maps-to-show-all-markers-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!