2012-05-05 24 views
0

我使用MapView.addView(),当它被轻敲显示在一个OverlayItem标记的顶部弹出气泡。有时弹出视图超出,因为所含信息或因为被窃听的标记是靠近地图的边缘量的显示屏幕的边缘。我怎么能确定何时所以显示整个弹出视图弹出视图将不适合再调整地图的位置?调整MapView类来显示整个弹出气泡

回答

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

我希望这可以帮助别人那里。

相关问题