在java中谷歌地图上自动缩放? (取决于安卓屏幕分辨率)

发布于 2024-08-29 01:41:58 字数 125 浏览 2 评论 0 原文

我有 2 个 GeoPoints 可以用标记在地图上显示它们。

如何获得 MapController 的最佳缩放级别,以便聚焦两个点的中间,同时将它们显示在地图上。

整个事情应该在不同的屏幕分辨率下工作。

I've got 2 GeoPoints given to show them on the map with markers.

How can I get the optimum zoom level for the MapController in order to focus the middle of both points, but also have them on the map.

The whole thing should work at different screen resolutions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光沙漏 2024-09-05 01:41:58

您可以使用 MapView 的 MapController。您可以使用 MapView.getController(),然后您可以尝试使用 MapController.zoomToSpan() 设置正确的缩放。我还倾向于使用 ItemizedOverlay 的 getLatSpanE6() 和 getLonSpanE6() 来简化此操作。一个例子:

MapView map = (MapView) findViewById(R.id.Map);
MapController mc = map.getController();
mc.zoomToSpan(overlay.getLatSpanE6(), overlay.getLonSpanE6());

重要的是要注意他们的警告:

因为缩放只能实现
离散水平,并且因为
地图的纵横比可能不匹配
给定的比例,质量
适合度可能会有所不同。我们唯一的事
保证的是,在缩放之后,在
新纬度或
新的经度将在一个因素内
来自相应参数的 2。

编辑:为了让地图缩放到正确的位置,您必须使用 MapController.setCenter()。

You can do this using the MapView's MapController. You get the MapController using MapView.getController(), then you can attempt to set the correct zoom using MapController.zoomToSpan(). I also tend to use ItemizedOverlay's getLatSpanE6() and getLonSpanE6() to make this simpler. An example:

MapView map = (MapView) findViewById(R.id.Map);
MapController mc = map.getController();
mc.zoomToSpan(overlay.getLatSpanE6(), overlay.getLonSpanE6());

It is important to note their caveat:

Because the zoom can only achieve
discrete levels, and because the
aspect ratio of the map may not match
the ratio given, the quality of the
fit may vary. The only thing we
guarantee is that, after the zoom, at
least one of the new latitude or the
new longitude will be within a factor
of 2 from the corresponding parameter.

Edit: In order to also get the map to zoom on the correct place, you have to use MapController.setCenter().

甜点 2024-09-05 01:41:58

这是应该如何完成的:

private void loadMarkerPositions() {


    for (Stores store : storeList) {
        latlong1 = new LatLng(store.getLatitude(), store.getLongtitude());
        array.add(latlong1);
    }

     List<LatLng> points = array; 

     final LatLngBounds.Builder bc = new LatLngBounds.Builder();

        for (LatLng item : points) {
            bc.include(item);
        }

    googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {

        @Override
        public void onCameraChange(CameraPosition arg0) {
            // Move camera.
            googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bc.build(), 30));
            // Remove listener to prevent position reset on camera move.
            googleMap.setOnCameraChangeListener(null);
        }
    });

}

参考:moveCamera with CameraUpdateFactory.newLatLngBounds crashes

有另一种简单的方法是,您可以使用此项目来完成所有与地图相关的活动:

https:// github.com/girishnair12345/Google-Maps-V2-library

This is how it should be done:

private void loadMarkerPositions() {


    for (Stores store : storeList) {
        latlong1 = new LatLng(store.getLatitude(), store.getLongtitude());
        array.add(latlong1);
    }

     List<LatLng> points = array; 

     final LatLngBounds.Builder bc = new LatLngBounds.Builder();

        for (LatLng item : points) {
            bc.include(item);
        }

    googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {

        @Override
        public void onCameraChange(CameraPosition arg0) {
            // Move camera.
            googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bc.build(), 30));
            // Remove listener to prevent position reset on camera move.
            googleMap.setOnCameraChangeListener(null);
        }
    });

}

Reference: moveCamera with CameraUpdateFactory.newLatLngBounds crashes

There is another easy way, you can use this project to accomplish all map related activities:

https://github.com/girishnair12345/Google-Maps-V2-library

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文