【问题标题】:Android Map Zoom to Show all PinsAndroid 地图缩放以显示所有引脚
【发布时间】:2010-08-26 15:30:54
【问题描述】:

我在地图上添加了 5 个图钉。如何让 MapView 尽可能放大并保持所有图钉都在视图中?

【问题讨论】:

  • 链接已失效。请更新。

标签: android android-mapview


【解决方案1】:

如果您使用ItemizedOverlay,这是更容易的解决方案

    public void fitOverlays() {
        mMapView.getController().zoomToSpan(mItemizedOverlay.getLatSpanE6(), mItemizedOverlay.getLonSpanE6());
}

【讨论】:

  • 我猜无论何时添加更多叠加层,或者如果有缩放以适应按钮。确实是您的设计问题。
【解决方案2】:

我在最近的一个项目中使用了这种方法

public void centerOverlays() {
    int minLat = 81 * MapStoresController.MAP_SCALE;
    int maxLat = -81 * MapStoresController.MAP_SCALE;
    int minLon = 181 * MapStoresController.MAP_SCALE;
    int maxLon = -181 * MapStoresController.MAP_SCALE;

    for (int i = 0; i < overlayItems.size(); i++) {
        Store s = overlayItems.getItem(i).getStore();
        minLat = (int) ((minLat > (s.getLocation().getLatitude() * MapStoresController.MAP_SCALE)) ? s.getLocation().getLatitude() * MapStoresController.MAP_SCALE :    minLat);
        maxLat = (int) ((maxLat < (s.getLocation().getLatitude() * MapStoresController.MAP_SCALE)) ? s.getLocation().getLatitude() * MapStoresController.MAP_SCALE : maxLat);
        minLon = (int) ((minLon > (s.getLocation().getLongitude() * MapStoresController.MAP_SCALE)) ? s.getLocation().getLongitude() * MapStoresController.MAP_SCALE : minLon);
        maxLon = (int) ((maxLon < (s.getLocation().getLongitude() * MapStoresController.MAP_SCALE)) ? .getLocation().getLongitude() * MapStoresController.MAP_SCALE : maxLon);
    }

    GeoPoint gp = controller.getUserLocation();

    minLat = (minLat > gp.getLatitudeE6()) ? gp.getLatitudeE6() : minLat;
    maxLat = (maxLat < gp.getLatitudeE6()) ? gp.getLatitudeE6() : maxLat;
    minLon = (minLon > gp.getLongitudeE6()) ? gp.getLongitudeE6() : minLon;
    maxLon = (maxLon < gp.getLongitudeE6()) ? gp.getLongitudeE6() : maxLon;

    mapView.getController().zoomToSpan((maxLat - minLat), (maxLon - minLon));
    mapView.getController().animateTo(new GeoPoint((maxLat + minLat) / 2, (maxLon + minLon) / 2));
}

基本上它只是找到一个框的边界并将视图设置为最近的包含边界的视图

【讨论】:

  • MapStoresController 是在哪里定义的?我找不到它的参考,它是 google map api 的一部分吗?
  • 这是我自己实现的。 MAP_SCALE 属性定义为:public static final int MAP_SCALE = (int) 1E6;
  • Schwiz 下面的解决方案要好得多
猜你喜欢
  • 1970-01-01
  • 2013-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
相关资源
最近更新 更多