2012-05-05 50 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); 
    } 
}; 

我希望這可以幫助別人那裏。

相關問題