【问题标题】:Zoom HERE maps to show all markers AndroidZoom HERE 地图以显示所有标记 Android
【发布时间】:2020-01-23 03:34:27
【问题描述】:

我在 Android 应用的 HERE 地图上显示了多个位置。总共有 4 个标记,其中 3 个标记在一个城市,1 个在另一个城市。目前,我的地图缩放到一个城市中只有 3 个标记可见,而第 4 个不可见的级别。为了看到那个标记,我必须缩小相当大的水平。

有没有办法,我可以显示地图范围内的所有标记?

这是我的代码:-

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

编辑

在 Rahul 的回答的帮助下,我实现了这样的目标:-

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

现在,地图在地图中心放大,周围的所有位置。但是,这个位置没有标记,所以我必须缩小一点才能看到所有标记。我该如何解决这个问题?

非常感谢任何帮助。

【问题讨论】:

标签: android maps here-maps-rest


【解决方案1】:

我用这个实现了同样的目标。

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

【讨论】:

    【解决方案2】:

    您必须使用 CameraUpdate 类来执行(可能)所有程序化地图移动。

    为此,首先计算所有标记的边界,如下所示:

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

    然后使用工厂获取一个运动描述对象:CameraUpdateFactory 如下:

    int padding = 0; // offset from edges of the map in pixels
    
    
    CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
    

    现在我们可以在 Map 中使用它,如下所示:

    googleMap.moveCamera(cu);
    

    【讨论】:

    • 感谢您的回答。我使用 HERE 地图来显示地图,而不是 Google 地图。
    • @PoojaGaonkar 这里您使用的是哪个地图,是自定义地图。?
    【解决方案3】:

    您可以使用LatLngBounds.Builder() 制作多个标记以在地图下查看。

    它有一个方法include,允许您添加多个 LAT LNG 位置。

    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)
    

    编辑

    在地图中添加标记时创建一个新列表

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

    【讨论】:

    • 感谢拉胡尔的回答。如何使用 HERE 地图进行这项工作?
    • @PoojaGaonkar 我在回答中添加了一些信息。立即查看
    • 再次感谢。 mmap 是您的 Here 地图对象吗?
    • @PoojaGaonkar 是的。
    • 我的 HERE 地图对象没有获得 animateCamera()。我必须为此包含一些依赖项吗?
    【解决方案4】:

    根据这个问题:https://github.com/heremaps/here-android-sdk-examples/issues/176 你需要像这样缩放:

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

    【讨论】:

      【解决方案5】:

      在应用缩放之前,您需要在地图上设置填充

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

      然后你创建 GeoBoundingBox 并实现缩放,或者如果你得到一个计算出的路线,那么你可以通过这种方式得到它:

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

      我正在使用 HERE 地图 3.18.2 版本

      【讨论】:

        【解决方案6】:

        您可以使用计算出的地图路线的boundingBox。

        val boundingBox = mapRoute?.route?.boundingBox
        

        或者如果它为空或者你想为特定的标记创建boundingBox,使用这个方法:

        val geoCoordinateList = arrayListOf<GeoCoordinate>()
        selectedMarkersOrLocations.forEach {
            geoCoordinateList.add(GeoCoordinate(it.latitude?:0.0, it.longitude?:0.0))
        }
        val boundingBox = GeoBoundingBox.getBoundingBoxContainingGeoCoordinates(geoCoordinateList)
        

        然后用填充设置缩放到boundingBox:

        val dpPadding = 100   //you can change this
        val padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpPadding.toFloat(), resources.displayMetrics)
        
        if (boundBox != null && (mHereMap?.width != 0 || mHereMap?.height != 0)){
            val viewRect = ViewRect(padding, padding, (mHereMap?.width?:0) - padding * 2, (mHereMap?.height?:0) - padding * 2)
            map.zoomTo(boundBox, viewRect, Map.Animation.LINEAR, Map.MOVE_PRESERVE_ORIENTATION)
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-01-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-12-30
          • 1970-01-01
          • 2022-06-20
          相关资源
          最近更新 更多