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

Android 调整MapView以显示整个弹出气泡,android,Android,我正在使用MapView.addView()在点击覆盖项标记时在其顶部显示一个弹出气泡。有时,弹出视图会超出显示屏幕的边缘,因为它包含的信息量很大,或者因为点击的标记靠近地图的边缘。我如何确定弹出视图何时不合适,然后调整地图的位置以显示整个弹出视图?我问这个问题已经三个月了,所以我想我会发布我提出的解决方案,以防有人看到这个帖子。此方法是扩展MapView的类的一部分 /** * Repositions the map, if necessary, in order to fully disp

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

我问这个问题已经三个月了,所以我想我会发布我提出的解决方案,以防有人看到这个帖子。此方法是扩展
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));
  }
}
下面是听众:

/**
 * 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);
  }
};
我希望这能帮助其他人

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