【问题标题】:Adjust MapView to display entire pop-up bubble调整 MapView 以显示整个弹出气泡
【发布时间】:2012-05-05 15:47:01
【问题描述】:

我正在使用 MapView.addView() 在点击 OverlayItem 标记时在其顶部显示一个弹出气泡。有时,弹出视图会超出显示屏幕的边缘,因为它包含的信息量很大,或者因为被点击的标记靠近地图的边缘。如何确定弹出视图何时不适合,然后调整地图的位置以显示整个弹出视图?

【问题讨论】:

    标签: android


    【解决方案1】:

    我问这个问题已经三个月了,所以我想我会发布我想出的解决方案,以防万一有人看到这个帖子。此方法是扩展 MapView 的类的一部分。

    /**
     * Repositions the map, if necessary, in order to fully display the view passed in.  Mainly used
     * by the map bubbles displayed when tapping on an overlay item marker.  It's important that the
     * view passed in has already been laidout by the system so its measurements are available.
     *
     * @param view The being displayed on the map.
     */
    private void recenter(View view) {
      Display display = windowManager.getDefaultDisplay();
    
      // Get the dimensions of the physical display screen.  These will differ based on the orientation
      // of the device.
      int displayWidth  = display.getWidth();
      int displayHeight = display.getHeight();
    
      // Get the coordinates of the top left and bottom right corners of the view.
      Point topLeft     = new Point(view.getLeft(),  view.getTop());
      Point bottomRight = new Point(view.getRight(), view.getBottom());
    
      // Will be used to convert between map coordinates and pixels.
      Projection projection = getProjection();
    
      // Get the pixel coordinates for the map's current center and use that to initialize a new point
      // object that will be used to reposition the map if necessary.
      Point mapCenter = projection.toPixels(getMapCenter(), null);
      Point newCenter = new Point(mapCenter);
    
      // Adjust the "x" coordinate if necessary.
      if (topLeft.x < 0) {
        newCenter.x += topLeft.x;
      }
      else if (bottomRight.x > displayWidth) {
        newCenter.x += bottomRight.x - displayWidth;
      }
    
      // Adjust the "y" coordinate if necessary.
      if (topLeft.y < 0) {
        newCenter.y += topLeft.y;
      }
      else if (bottomRight.y > displayHeight) {
        newCenter.y += bottomRight.y - displayHeight;
      }
    
      // Reposition the map only if the newly calculated center differs from the original.
      if (!newCenter.equals(mapCenter)) {
        getController().animateTo(projection.fromPixels(newCenter.x, newCenter.y));
      }
    }
    

    让我感到困惑的一件事是如何确保传入的视图具有可用的测量值。我通过注册一个监听器来解决这个问题,以确定系统何时布置了视图,如下所示:

    // Add a listener to determine when the layout for the "mapBubbleMsg" view has been completed.
    // This will be used to reposition the map if necessary so the newly added view is visible.
    mapBubbleMsg.getViewTreeObserver().addOnGlobalLayoutListener(mapBubbleMsgLayoutListener);
    

    这是听者:

    /**
     * Listen for layout changes to the "mapBubbleMsg" view.  This will be used to reposition the map
     * if necessary so the "mapBubbleMsg" view is visibile.  The first thing this listener does is
     * unregister itself in order to prevent it from being called repeatedly each time the "mapBubbleMsg"
     * view layout changes.
     */
    ViewTreeObserver.OnGlobalLayoutListener mapBubbleMsgLayoutListener = new
    ViewTreeObserver.OnGlobalLayoutListener() {
      public void onGlobalLayout() {
        mapBubbleMsg.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        recenter(mapBubbleMsg);
      }
    };
    

    我希望这对其他人有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-11
      • 1970-01-01
      • 2016-02-13
      • 2016-03-25
      • 1970-01-01
      • 2010-12-06
      相关资源
      最近更新 更多