簡體   English   中英

(Android) 創建帶有地圖標記的邊界框並在 Google Map V2 中獲取其寬度

[英](Android) Create bounding box with map markers and get width of it in Google Map V2

我正在使用以下代碼在 Android 中的 GoogleMap 上縮放和顯示所有標記。 它運作良好:

LatLngBounds.Builder builder = new LatLngBounds.Builder();

//the include method will calculate the min and max bound.
builder.include(marker1.getPosition());
builder.include(marker2.getPosition());
builder.include(marker3.getPosition());
builder.include(marker4.getPosition());

LatLngBounds bounds = builder.build();

int width = getResources().getDisplayMetrics().widthPixels;
int height = getResources().getDisplayMetrics().heightPixels;
int padding = (int) (width * 0.10); // offset from edges of the map 10% of screen

CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, width, height, padding);

mMap.animateCamera(cu);

但我想要做的是將所有標記映射到一個邊界框中,然后獲取該框的寬度。 我怎樣才能做到這一點?

您需要使用bounds.northeastbounds.southwest坐標

邊界尺寸

並使用Location.distanceBetween()方法或任何其他方法找到它們之間的距離:

float[] results = new float[1];
Location.distanceBetween(bounds.northeast.latitude, bounds.southwest.longitude,
            bounds.northeast.latitude, bounds.northeast.longitude,
            results);
float width_in_meters = results[0];

修改這一行LatLngBounds bounds = builder.build(); 對此

LatLngBounds bounds = googleMap.getProjection()
.getVisibleRegion().latLngBounds;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM