调整MapView以显示整个弹出式气泡

时间:2012-05-05 15:47:02

标签: android

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

1 个答案:

答案 0 :(得分:2)

自从我提出这个问题已经三个月了,所以我想我会发布我提出的解决方案,万一有人遇到这个帖子。此方法是扩展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);
  }
};

我希望这有助于其他人。

相关问题