简体   繁体   English

(Android) 创建带有地图标记的边界框并在 Google Map V2 中获取其宽度

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

I'm using the following code to zoom and show all markers on a GoogleMap in Android.我正在使用以下代码在 Android 中的 GoogleMap 上缩放和显示所有标记。 It works well:它运作良好:

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

But what I want to do is map all the markers in a bounding box and then get the width of that box.但我想要做的是将所有标记映射到一个边界框中,然后获取该框的宽度。 How can I do that?我怎样才能做到这一点?

You need to use bounds.northeast and bounds.southwest coordinates您需要使用bounds.northeastbounds.southwest坐标

边界尺寸

and find distance between them using Location.distanceBetween() method or any other :并使用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];

Modify this line LatLngBounds bounds = builder.build();修改这一行LatLngBounds bounds = builder.build(); to this对此

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM